C#_Regex


            //Regex.IsMatch();//用来判断给定的字符串是否匹配某个正则表达式
            //Regex.Match();//用来从给定的字符串中按照正则表达式的要求提取【一个】匹配的字符串
            //Regex.Matches();//用来从给定的字符串中按照正则表达式的要求提取【所有】匹配的字符串
            //Regex.Replace(); //替换所有正则表达式匹配的字符串为另外一个字符串。

有关元字符 请看这篇文章:  http://www.cnblogs.com/entclark/p/7802060.html

小练习:

1.请用户输入一个10(含)-25(含)之间的任何一个数字
   正则表达式:"^(1[0-9]|2[0-5])$"  
2.验证是否是合法的手机号
   正则表达式:@"^\d{11}$"
3.身份证号码
   //1.长度为15位或者18位的字符串,首位不能是0。
  //2.如果是15位,则全部是数字。
 //3.如果是18位,则前17位都是数字,末位可能是数字也可能是X。 
 正则表达式:"^[1-9]\d{16}[0-9xX]|[1-9]\d{14}$",
 //[num or other],数字或者其它字符都可以,表示其中的一个;
4.验证是否为合法的邮件地址
 正则表达式: @"^[-0-9a-zA-Z_\.]+@[a-zA-Z0-9]+(\.[a-zA-Z]+){1,2}$"
 //出现多个,碰到指定字符就截止,最好是[xxx]+@;不用(xxx)+@
5.匹配IP地址,4段用.分割的最多三位数字
   正则表达式:@"^([0-9]{1,3}\.){3}[0-9]{1,3}$"
6.判断是否是合法的日期格式“2008-08-08”。四位数字-两位数字-两位数字
    正则表达式: @"^[0-9]{4}-[0-9]{2}-[0-9]{2}$
7.判断是否是合法的url地址
   正则表达式:@"^[a-zA-Z0-9]+://.+$ 

字符串提取:(一般字符串提取不加^ 和 $,不限制)

1.Regex.Match只能提取一个匹配,结果是2010
 string msg = "大家好呀,hello,2010年10月10日是个好日子。恩,9494.吼吼!886.";
 Match match = Regex.Match(msg, "[0-9]+");
2.Regex.Matches()提取字符串中的所有匹配
 MatchCollection matches = Regex.Matches(msg, "[0-9]+");
 //结果是2010,10,10,9494,886
3.组, 在正则表达式中只要出现了()就表示进行了分组。小括号既有改变优先级的作用又具有提取组的功能。
 string html = File.ReadAllText("regex.htm");
 MatchCollection matches = Regex.Matches(html, @"([-a-zA-Z_0-9.]+)@([-a-zA-Z0-9_]+(\.[a-zA-Z]+)+)");
            foreach (Match item in matches)
            {
                //item.Value表示本次提取到的字符串
                //item.Groups集合中存储的就是所有的分组信息。
                //item.Groups[0].Value与item.Value是等价的都表示本次提取到的完整的字符串,表示整个邮箱字符串,而item.Groups[1].Value则表示第一组的字符串。
                //Console.WriteLine(item.Value);
                Console.WriteLine("第0组:{0}", item.Groups[0].Value);
                Console.WriteLine("第1组:{0}", item.Groups[1].Value);
                Console.WriteLine("第2组:{0}", item.Groups[2].Value);
                Console.WriteLine("===============================================");
            }
Console.WriteLine(matches.Count);
4.从“June26       ,        1951    ”中提取出月份June、26、1951来。
解析:月份和日之间是必须要有空格分割的,所以使用空白符号“\s”匹配所有的空白字符,此处的空格是必须有的,所以使用“+”标识为匹配1至多个空格。之后的“,”与年份之间的空格是可有可无的,所以使用“*”表示为匹配0至多个
 string date = "June26     ,        1951    ";
            Match match = Regex.Match(date, @"([a-zA-Z]+)\s*([0-9]{2})\s*,\s*([0-9]{4})\s*");
            for (int i = 0; i < match.Groups.Count; i++)
            {
                Console.WriteLine(match.Groups[i].Value);
            }
            Console.ReadKey();

5.从Email中提取出用户名和域名,比如从test@163.com中提取出test和163.com。
Console.WriteLine("请输入Email:");
            string email = Console.ReadLine();
            Match match = Regex.Match(email, @"(.+)@(.+)");
            Console.WriteLine("用户名:{0},域名:{1}", match.Groups[1].Value, match.Groups[2].Value);
6.192.168.10.5[port=21,type=ftp]”,这个字符串表示IP地址为192.168.10.5的服务器的21端口提供的是ftp服务,其中如果“,type=ftp”部分被省略,则默认为http服务。请用程序解析此字符串,然后打印出“IP地址为***的服务器的***端口提供的服务为***
            // string msg = "192.168.10.5[port=21,type=ftp]";
            string msg = "192.168.10.5[port=21]";
            Match match = Regex.Match(msg, @"(.+)\[port=([0-9]{2,5})(,type=(.+))?\]");
            Console.WriteLine("ip:{0}", match.Groups[1].Value);
            Console.WriteLine("port:{0}", match.Groups[2].Value);
            Console.WriteLine("services:{0}", match.Groups[4].Value.Length == 0 ? "http" : match.Groups[4].Value);
            Console.ReadKey();

反向引用

①使用$n替换正则中的第n组
//将hello ‘welcome’ to ‘China’   替换成 hello 【welcome】 to 【China】
    string msg = "hello 'welcome' to 'China'  'lss'   'ls'   'szj'   ";
            msg = Regex.Replace(msg, "'(.+?)'", "【$1】$$1");//2个$$代表一个,转义了
            Console.WriteLine(msg);
            Console.ReadKey();
//===================================
 //隐藏手机号码
            string msg = "xxx13409876543yyy18276354908aa87654321345bb98761234654";

            msg = Regex.Replace(msg, @"([0-9]{3})[0-9]{4}([0-9]{4})", "$1****$2");
            Console.WriteLine(msg);
//======隐藏邮箱名=====================
② 使用\n,表示引用第n组的内容,下面例子:(.)\1+,表示(.)这个组的内容,如X,\1引用了这组,+表示引用这组内容至少出现1次,所以匹配到的就是XXXXXYYYYYYZZZZZZZ ,$1替换成第一组的内容X;
    string msg = "你们喜欢XXXXXYYYYYYZZZZZZZ";
 msg = Regex.Replace(msg, @"(.)\1+", "$1");//Replace最后一个参数还可以对我们输入的正则进行操作
            Console.WriteLine(msg);//你们喜欢zyz
            Console.ReadKey();
③(.)出现了一次,接着\1,对组的内容又出现一次,所以就能匹配AA

            string msg = "AABB";
            MatchCollection matches = Regex.Matches(msg, @"(.)\1(.)\2");
            foreach (Match item in matches)
            {
                Console.WriteLine(item.Value);
            }
④将一段文本中的MM/DD/YYYY格式的日期转换为YYYY-MM-DD格式 ,比如“我的生日是05/21/2010耶”转换为“我的生日是2010-05-21耶”。
 string msg = "我的生日是05/21/2010耶我的生日是05/21/2010耶";
            msg = Regex.Replace(msg, @"(\d{2})/(\d{2})/(\d{4})", "$3-$1-$2", RegexOptions.ECMAScript);
            Console.WriteLine(msg);
⑤  给一段文本中匹配到的url添加超链接,比如把http://www.test.com替换为<a href="http://www.test.com"> http://www.test.com</a>。因为这个是整体做为一个组,比较特殊.
 string msg = "给一段文本中匹配到的url添加超链接,比如把http://www.test.com替换http://www.sina.com.cn哈哈http://www.google.com";
            msg = Regex.Replace(msg, @"[a-zA-Z0-9]+://[-a-zA-Z0-9.?&=#%\/_]+", "<a href=\"$0\">$0</a>");
            Console.WriteLine(msg);
            Console.ReadKey();

单词边界

 

A:
            string msg = "The day after tomorrow is my wedding day.The row we are looking for is .row. number 10.";
            //\b :表示单词的边界。
            //什么叫做“单词”? [a-zA-Z0-9_]
            msg = Regex.Replace(msg, @"\brow\b", "line");
            Console.WriteLine(msg);
            Console.ReadKey();
B:
 //请提取出3个字母的单词。
            string msg = "Hi,how are you?Welcome to our country!";
            MatchCollection matches = Regex.Matches(msg, @"\b[a-z]{3}\b", RegexOptions.IgnoreCase);
            foreach (Match item in matches)
            {
                Console.WriteLine(item.Value);
            }
            Console.ReadKey();

 

  

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM