?【C#】判斷IP地址是否合法源碼
/// <summary> /// 匹配IP地址是否合法 /// </summary> /// <param name="ip">當(dāng)前需要匹配的IP地址</param> /// <returns>true:表示合法</returns> public static bool MatchIP(string ip) { bool success = false; if (!string.IsNullOrEmpty(ip)) { //判斷是否為IP success = Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$"); } return success; }
/// <summary>
/// 匹配IP地址是否合法
/// </summary>
/// <param name="ip">當(dāng)前需要匹配的IP地址</param>
/// <returns>true:表示合法</returns>
public static bool MatchIP(string ip)
{
bool success = false;
if (!string.IsNullOrEmpty(ip))
//判斷是否為IP
success = Regex.IsMatch(ip, @"^((2[0-4]\d|25[0-5]|[01]?\d\d?)\.){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)$");
}
return success;