正则表达式匹配IP static void Main(string[] args)
{
//择⼀匹配,查数字或字母
//string s="ad是是fs地dff22天{!@!~}}sdfsdffffcz";
//string pattern =@"\d|[a-z]";//表达式(匹配所有数字或⼩写字母)
//MatchCollection col = Regex.Matches(s, pattern);//Matches⽅法,每⼀个匹配上的结果存⼊col中。            //foreach (Match m in col)//遍列出col中的Match
正则匹配开头//{
//    Console.WriteLine(m);
//}
//将⼈名输出
//string s1 = "zhangsan;lisi,wangwu.zhaolliu";
//// string pattern1 = @"[;,.]";//[]匹配
//string pattern1 = @"[;]|[,]|[.]";//择⼀匹配
//string[] res= Regex.Split(s1, pattern1);
//foreach (string s2 in res)
//{
//    Console.WriteLine(s2);
//}
//校验国内电话号码是否合法(⽅式⼀:010-********;⽅式⼆:(010)87654321)
//string[] TellNumBer = new string[8];
//TellNumBer[0] = "(010)87654321";
//TellNumBer[1] = "010-********";
/
/TellNumBer[2] = "010********";
//TellNumBer[3] = "0908*******";
//TellNumBer[4] = "(090)87654321";
//TellNumBer[5] = "090-87654321";
//TellNumBer[6] = "(0908*******";
//TellNumBer[7] = "91287654321";
//Regex RegexTellNumber = new Regex(@"\(0\d{2,3}\)\d{7,8}|^0\d{2,3}\-\d{7,8}$");
//foreach (string Tell in TellNumBer)
//{
//    Console.WriteLine(Tell + "是否合法:" + RegexTellNumber.IsMatch(Tell));
//}
/
/重复多个字符使⽤(abcd){n}进⾏分组限定
//string strGroup = @"(ab\w{2}){2}";//ab+两个数字并重复两次
//string s3 = "ab12ab3233ab34ab45";
//Regex RegexGroup = new Regex(strGroup);
//Console.WriteLine(RegexGroup.Replace(s3,"replace"));//将满⾜条件的替换掉
//校验IP4地址是否合法
//2[0-4]\d      第⼀位为2时,第⼆位可为0-4
//25[0-5]  或者⼀⼆位为25时,第三位为0-5
//[01]?\d\d?  或者第⼀位的0或1出现1次或0次时,第⼆\d,第三位\d出现0或1次
//\. 点
//{3}上⾯的重复出现3次
/
/(2[0-4]\d|25[0-5]|[01]?\d\d?) 判断第4位IP的情况
string PatternIP = @"^(((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?))$";
string IP = "8.168.1.111";
Regex RegexIp = new Regex(PatternIP);
Console.WriteLine(RegexIp.IsMatch(IP));
Console.ReadKey();
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。