正則表達式匹配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-87654321;方式二:(010)87654321)
            //string[] TellNumBer = new string[8];
            //TellNumBer[0] = "(010)87654321";
            //TellNumBer[1] = "010-87654321";
            //TellNumBer[2] = "01087654321";
            //TellNumBer[3] = "09087654321";
            //TellNumBer[4] = "(090)87654321";
            //TellNumBer[5] = "090-87654321";
            //TellNumBer[6] = "(09087654321";
            //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();


        }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM