现在的位置: 首页 > 综合 > 正文

判断IP地址是否有效 (正则表达式)

2014年01月22日 ⁄ 综合 ⁄ 共 796字 ⁄ 字号 评论关闭

 

public bool IsCorrenctIP(string strIP)

{

    string strRegExp = @"(/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(strIP, strRegExp)) return true;

    else return false;

}

 

public bool IsValidIP(string strIP)

{

    if (System.Text.RegularExpressions.Regex.IsMatch(strIP, "[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}//.[0-9]{1,3}"))

    {

        string[] ip_ = strIP.Split('.');

        if (ip_.Length == 4 || ip_.Length == 6)

        {

            if (System.Int32.Parse(ip_[0]) < 256 && System.Int32.Parse(ip_[1]) < 256 & System.Int32.Parse(ip_[2]) < 256 & System.Int32.Parse(ip_[3]) < 256) return true;

            else return false;

        }

        else return false;

    }

    else return false;

}

 

抱歉!评论已关闭.