2009-03-11 [C#]判斷IP地址是否有效 (正規運算式) 9221 0 C# 2009-04-03 摘要:[C#]判斷IP地址是否有效 (正規運算式) 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; } C#.NETIP正則表達式 回首頁