C#两个IP地址正则

1、

 1 public   bool   IsCorrenctIP(string ip)
 2   {
    //网上很多都是错的,这个才是正确滴
3 string pattrn=@"^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$"; 4 if(System.Text.RegularExpressions.Regex.IsMatch(ip,pattrn)) 5 { 6 return true; 7 } 8 else 9 { 10 return false; 11 12 } 13 }

2、

 1   public   bool   IsValidIP(string   ip)   
 2   {   
 3    if   (System.Text.RegularExpressions.Regex.IsMatch(ip,"[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}"))   
 4    {   
 5     string[]   ips   =   ip.Split('.');   
 6     if   (ips.Length   ==   4   ||   ips.Length   ==   6)   
 7     {   
 8      if(System.Int32.Parse(ips[0])   <   256   &&   System.Int32.Parse(ips[1])   <   256   &   System.Int32.Parse(ips[2])   <   256   &   System.Int32.Parse(ips[3])   <   256)   
 9       return   true;   
10      else   
11       return   false;   
12     }   
13     else   
14      return   false;     
15    }   
16    else   
17     return   false;   
18   }

 

posted @ 2013-05-08 13:30  百年  阅读(192)  评论(0)    收藏  举报