码迷,mamicode.com
首页 > 编程语言 > 详细

C#语言正则用法

时间:2017-06-13 11:30:58      阅读:202      评论:0      收藏:0      [点我收藏+]

标签:line   手机号   write   int   研究   正则表达式   pat   符号   nbsp   

string phone ="";

string pattern @"|\d{10}";

bool rusurt = false;

Console.WriteLine("请输入手机号");

do{

phone = Console.ReadLine();

rusurt = Rogex.IsMatch(phone,pattern);     

phone  是手机号,pattern    正则表达式

if( !result )

Console.WriteLine("BadNum,try again");

}

while (!result)

Console.WriteLine("Good");

 

正则实例研究:

static void Main(string[] args)
        {
            Regex reg1 = new Regex(@"\d+");
            string str = reg1.Replace("123","insert into table where id = $&");
            Console.WriteLine(str);     //输出 insert into table where id = 123

            Regex reg2 = new Regex(@"1\+1=(\d)");
            string str2 = reg2.Replace("1+1=3","不是$1");
            Console.WriteLine(str2);    //输出 不是3

            Regex reg3 = new Regex(@"1\+1=(?<result>\d)");
            string str3 = reg3.Replace("1+1=3", "不是${result}");
            Console.WriteLine(str3);    //输出 不是3

            Regex reg4 = new Regex(@"\d+");
            string str4 = reg4.Replace("123ABC", "后面是$‘");  //匹配文本之后的文本
            Console.WriteLine(str4);    //输出 后面是ABCABC     为什么会输出 后面是ABCABC呢?因为$‘指的是ABC,然后替换掉原字符串中的123。不懂看多几次这句话

            Regex reg5 = new Regex(@"\d+");
            string str5 = reg5.Replace("ABC123", "前面是$`");  //ABC前面是ABC 符号是 1左边那个
            Console.WriteLine(str5);

            Regex reg6 = new Regex(@"\d+");
            string str6 = reg6.Replace("ABC123","右边原始输入字符串$_");
            Console.WriteLine(str6);    //输出 右边是原始字符串ABC123

            Console.ReadKey();
        }

 

C#语言正则用法

标签:line   手机号   write   int   研究   正则表达式   pat   符号   nbsp   

原文地址:http://www.cnblogs.com/yunpeng521/p/6999673.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!