using System.Text.RegularExpressions;
public bool isIP(string ip)
{
string pattern = @"(((/d{1,2})|(1/d{2})|(2[0-4]/d)|(25[0-5]))/.){3}((/d{1,2})|(1/d{2})|(2[0-4]/d)|(25[0-5]))";
Regex objRe = new Regex(pattern);
Match objMa = objRe.Match(ip);
if(!objMa.Success)
{
return false;
}
else return true;
}
最后只需加上一个判断IP长度就完美了