码迷,mamicode.com
首页 > Windows程序 > 详细

C#_Regex

时间:2017-11-26 13:59:50      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:用户   line   端口   div   打印   pre   dal   完整   sha   

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

小练习:

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();

  

 

C#_Regex

标签:用户   line   端口   div   打印   pre   dal   完整   sha   

原文地址:http://www.cnblogs.com/entclark/p/7898671.html

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