Loading ...

Regex: Validate an IP address | CodeAsp.Net

Regex: Validate an IP address

 /5
0 (0votes)

In this blog I will show how to validate whether a string is a valid IP address not with REGEX. Below is the sample code to check the same:

using System;
using System.Text.RegularExpressions;

namespace SampleConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            const string inputString = "192.168.1.1";
            Regex regex = new Regex(@"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$", RegexOptions.Singleline | RegexOptions.IgnoreCase);


            Console.WriteLine(regex.IsMatch(inputString)
                ? "Valid IP" : "Invalid IP");

            const string inputString2 = "192.168.1.1.33";
            Console.WriteLine(regex.IsMatch(inputString2)
                ? "Valid IP" : "Invalid IP");

            Console.ReadLine();

        }
      
    }
}

Output:

Below is the explanation of regex:

aa

^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$

Assert position at the beginning of the string «^»
Match the regular expression below and capture its match into backreference number 1 «(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}»
   Exactly 3 times «{3}»
   Note: You repeated the capturing group itself.  The group will capture only the last iteration.  Put a capturing group around the repeated group to capture all iterations. «{3}»
   Match the regular expression below and capture its match into backreference number 2 «([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])»
      Match either the regular expression below (attempting the next alternative only if this one fails) «[0-9]»
         Match a single character in the range between “0” and “9” «[0-9]»
      Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[1-9][0-9]»
         Match a single character in the range between “1” and “9” «[1-9]»
         Match a single character in the range between “0” and “9” «[0-9]»
      Or match regular expression number 3 below (attempting the next alternative only if this one fails) «1[0-9]{2}»
         Match the character “1” literally «1»
         Match a single character in the range between “0” and “9” «[0-9]{2}»
            Exactly 2 times «{2}»
      Or match regular expression number 4 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
         Match the character “2” literally «2»
         Match a single character in the range between “0” and “4” «[0-4]»
         Match a single character in the range between “0” and “9” «[0-9]»
      Or match regular expression number 5 below (the entire group fails if this one fails to match) «25[0-5]»
         Match the characters “25” literally «25»
         Match a single character in the range between “0” and “5” «[0-5]»
   Match the character “.” literally «\.»
Match the regular expression below and capture its match into backreference number 3 «([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])»
   Match either the regular expression below (attempting the next alternative only if this one fails) «[0-9]»
      Match a single character in the range between “0” and “9” «[0-9]»
   Or match regular expression number 2 below (attempting the next alternative only if this one fails) «[1-9][0-9]»
      Match a single character in the range between “1” and “9” «[1-9]»
      Match a single character in the range between “0” and “9” «[0-9]»
   Or match regular expression number 3 below (attempting the next alternative only if this one fails) «1[0-9]{2}»
      Match the character “1” literally «1»
      Match a single character in the range between “0” and “9” «[0-9]{2}»
         Exactly 2 times «{2}»
   Or match regular expression number 4 below (attempting the next alternative only if this one fails) «2[0-4][0-9]»
      Match the character “2” literally «2»
      Match a single character in the range between “0” and “4” «[0-4]»
      Match a single character in the range between “0” and “9” «[0-9]»
   Or match regular expression number 5 below (the entire group fails if this one fails to match) «25[0-5]»
      Match the characters “25” literally «25»
      Match a single character in the range between “0” and “5” «[0-5]»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»


Created with RegexBuddy

Comments (no comments yet)

Top Posts