C#判断IP地址是否合法函数-使用正则表达式-2个

public   bool   IsCorrenctIP(string ip)
  {  
   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])";
   if(System.Text.RegularExpressions.Regex.IsMatch(ip,pattrn)) 
   {
    return   true;  
   }
   else
   {
    return   false;  
   
   }
  }


  public   bool   IsValidIP(string   ip)  
  {  
   if   (System.Text.RegularExpressions.Regex.IsMatch(ip,"[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}"))  
   {  
    string[]   ips   =   ip.Split('.');  
    if   (ips.Length   ==   4   ||   ips.Length   ==   6)  
    {  
     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)  
      return   true;  
     else  
      return   false;  
    }  
    else  
     return   false; 
   
   }  
   else  
    return   false;  
  } 

posted on 2007-07-12 15:24  风灵溪清  阅读(246)  评论(0编辑  收藏  举报

导航