以下是正则表达式:
#region //IPV4正则表达式函数 public static bool IsIPv4Address(string ip) { string pattern = @"^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]
|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]
|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$"; Regex r = new Regex(pattern); Match m = r.Match(ip); return m.Success; } #endregion
使用方法:
IPHostEntry IpEntry = Dns.GetHostEntry(Dns.GetHostName());
for (int i = 0; i != IpEntry.AddressList.Length; i++)
{
if (IsIPv4Address(IpEntry.AddressList[i].ToString()))
{
comboBox1.Items.Add(IpEntry.AddressList[i]);
}
}