1.解析IP
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
IP验证namespace 解析IP { class Program { static void Main(string[] args) { string str = "192.168.1.100[port=8080,type=ftp]"; Match m1 = Regex.Match(str, @"\d{0,3}(\.\d{0,3}){3}"); if (m1.Success) { Console.WriteLine("IP地址为{0}", m1.Value); } Match m2 = Regex.Match(str, @"port=(\d+)"); if (m2.Success) { Console.WriteLine("端口为{0}", m2.Groups[1].Value); } Match m3 = Regex.Match(str, @"type=(\w+)"); if (m3.Success) { Console.WriteLine("类型为{0}", m3.Groups[1].Value); } Console.ReadKey(); } } }
2.判断URL是否合法
判断URL是否合法namespace 判断URL是否合法 { class Program { static void Main(string[] args) { string str = "http://www.test.com/a.htm"; Match m = Regex.Match(str, @"[a-zA-z]+://[^\s]*"); if (m.Success) { Console.WriteLine("正确"); } else { Console.WriteLine("错误"); } Console.ReadKey(); } } }
3.判断日期是否合法
判断日期是否合法namespace 判断日期是否合法 { class Program { static void Main(string[] args) { string str="2012-12-21"; Match m = Regex.Match(str, @"^\d{4}\-((0[1-9])|(1[0-2]))-((0[1-9])|(1[0-9])|(2[0-9])|(3[0-1]))$"); if (m.Success) { Console.WriteLine("合法"); } else { Console.WriteLine("非法"); } Console.ReadKey(); } } }
4.匹配根目录下的文件夹
匹配根目录下的文件夹namespace 匹配根目录下的文件夹 { class Program { static void Main(string[] args) { string str = @"C:\Windows\System32\myCmd.dll"; MatchCollection ms = Regex.Matches(str, @"(\w+)\\"); foreach (Match m in ms) { if (m.Success) { Console.WriteLine("文件夹名字是{0}\t\r\n", m.Groups[1].Value); } } Match m1 = Regex.Match(str, @".+\\(.+)"); if (m1.Success) { Console.WriteLine("文件名是{0}", m1.Groups[1].Value); } Console.ReadKey(); } } }
5.匹配国内电话号码
匹配国内电话号码namespace 匹配国内电话号码 { class Program { static void Main(string[] args) { while (true) { Console.WriteLine("请输入号码,区号请用-区分"); string str = Console.ReadLine(); Match m1 = Regex.Match(str, @"^\d{3}-\d{8}$|^\d{4}-\d{7}$"); Match m2 = Regex.Match(str, @"^\d{11}$"); Match m3 = Regex.Match(str, @"^\d{5}$"); if (m1.Success) { Console.WriteLine("你输入的是电话号码{0}", m1.Value); } else if (m2.Success) { Console.WriteLine("你输入的是手机号码{0}", m2.Value); } else if (m3.Success) { Console.WriteLine("你输入的是服务号码{0}", m3.Value); } else { Console.WriteLine("输入错误"); } Console.WriteLine(); } Console.ReadKey(); } } }
6.匹配身份证号码
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
匹配身份证号码namespace 匹配身份证号码 { class Program { static void Main(string[] args) { while (true) { Console.WriteLine("请输入号码"); string ID= Console.ReadLine(); Match m1 = Regex.Match(ID, @"^\d{15}$"); Match m2=Regex.Match(ID,@"^\d{14}([0-9X]{4})$"); if (m1.Success) { Console.WriteLine("输入正确{0}",m1.Value); } else if (m2.Success) { Console.WriteLine("输入正确{0}", m2.Value); } else { Console.WriteLine("输入错误!"); } Console.WriteLine(); } Console.ReadKey(); } } }
7.提取数字
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
提取数字namespace 提取数字 { class Program { static void Main(string[] args) { string str = "hello,2010年10月10日是个好日子。恩,9494.吼吼!886."; MatchCollection ms = Regex.Matches(str, @"\d+"); foreach (Match m in ms) { if (m.Success) { Console.WriteLine("{0}\t\r\n", m.Value); } } Console.ReadKey(); } } }