Validate IP Address With Regular Expressions

Regular Expressions

A regular expression, or regex in short, is a string of text which represents a search pattern. They are usually used either to search through text and find strings which match the given pattern, or to validate a string against the search pattern.

Regular expressions are extremely powerful but they can prove quite difficult to understand and get the hang of. Once you do manage to get the hang of them, you will most likely find that they are great to use in your code and they can save you plenty of coding time.

In this article I will be showing you how to validate an IP v4 address using a regular expression. The .NET framework contains a class called System.Text.RegularExpressions.Regex which is just what we need to validate a string of text – in our case the IP v4 address.

Add the namespace System.Text.RegularExpressions to your project and then have a look at the below code which will validate our IP v4 address:

private bool ValidateIPAddress(string ipAddress)
    // Set the default return value to false
    bool isIPAddress = false;

    // Set the regular expression to match IP addresses
    string ipPattern = @"\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";

    // Create an instance of System.Text.RegularExpressions.Regex
    Regex regex = new Regex(ipPattern);

    // Validate the IP address
    isIPAddress = regex.IsMatch(ipAddress);

    return isIPAddress;

In the above code we are first assigning the regex pattern to a string for later use. Then we are creating an instance of the Regex class, and finally we are making use of the IsMatch property of the Regex class. IsMatch will tell us if the string we passed to it contains text that matches the regular expression. If IsMatch finds a match it will return true, and if not it will return false.

The following code shows how to use this ValidateIPAddress method we just created:

bool isValid = ValidateIPAddress("");

As you can see, the .NET framework makes working with regular expressions quite easy. The difficult part is the creating of the regular expression itself, but that is a whole article series on its own.

Dave runs on the Thesis Theme

How smart is your Theme? How good is your support? Check out ThesisTheme for WordPress.

The Thesis Theme is one of the best Wordpress frameworks out there, especially if you understand software development, which since you're here you probably do. Thesis allows you to design post and page templates using a drag-and-drop system which gives you access to all the relevant page elements you would normally have to manually access through code. This is why understanding development techniques definitely helps, even though you don't need to write any code to get Thesis up and running.

So go check out the incredible features at DIYThemes and start using Thesis now!

2 comments… add one
  • Flocke

    you can use:

    IPAddress ipValue;
    bool ipOk = IPAddress.TryParse(ip, out ipValue);

  • Yogesh

    Thanks, this is working for me

Leave a Comment