用正则表达式判断IP地址是否合法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/jingangxin666/article/details/80787632

先了解IP的格式,它的形式应该为:(1~255).(0~255).(0~255).(0~255)

//ip地址范围:(1~255).(0~255).(0~255).(0~255)
private static Regex _ipReg = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$");
public static bool ValidateIPString(string ip)
{
    return !string.IsNullOrEmpty(ip) && _ipReg.IsMatch(ip);
}

猜你喜欢

转载自blog.csdn.net/jingangxin666/article/details/80787632