C# - Determines whether the specified string is an IP address

By , 9/16/2008
(1 ratings)
Determines whether the specified string is an IP address using Regex.

Example:

"0.0.0.0" bis "255.255.255.255" valid
"1.2.3.256" oder "0.0.2" invalid
Translate to VB
/// <summary>
/// Determines whether the specified string is an IP address.
/// </summary>
/// <param name="IP">The string.</param>
/// <returns>
/// 	<c>true</c> if the specified IP is IP; otherwise, <c>false</c>.
/// </returns>
private bool IsIP(string IP)
{
    return System.Text.RegularExpressions.Regex.IsMatch(IP, @"\b((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$\b");
}
Tagged with Regex, IP, string.

Comments

 

Log in, to comment!