Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

help with ip address regular expression

Last post 02-07-2010, 6:20 PM by Aussie Susan. 1 replies.
Sort Posts: Previous Next
  •  02-06-2010, 9:25 AM 59343

    help with ip address regular expression

    I'm very new to regular expressions so bear with me if I sound as if I dont have a clue.

    I'm developing an application in C#, using .NET 3.5 Visual Studio 2008

    I need a regular expression that is capable of validating an IP address correctly, I'm aware that there are IP address validation expressions on this site but they just regard any address between 0.0.0.0 and 255.255.255.255 as valid which is not what I need.

    For anyone familiar with IP addressing to ensure the address is valid for my app it must be class A, B or C or basically the first octet (the first set of numbers before the first dot) must be between 0 and 223, any of the other octets can be 0-255

    I've searched a few sites and found a pretty good expression and modified the first part of it so it filters from 0 to 223 which is what I want. Here is the expression after I modified the first bit:

    @"^(22[0-2]|2[0-2][0-3]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";

    ;

    It does work pretty well, however it does have a flaw, if a 0 character is entered in any octet then subsequent zeros are accepted, therefore you can enter 192.000.000.000 and the address will be marked as valid or even 192.001.04.000 all these addresses should not be validated. I need an expression that will not accept more than one zero if the zero is entered first for each octet. So two zeros like this would be acceptable 192.100.100.100 but not 192.001.001.001

    Could anyone tell me how the expression could be modified to do this, if it is at all possible? In addtion addresses with an octet starting with 127 (loopback) should really be filtered, I can do this programatically, but would there be a way of incorporating this into the expression as well so any address starting with 127 such as 127.34.45.2 would be invalid.

    any help would be very much appreciated.

    many thanks,

    Paul

  •  02-07-2010, 6:20 PM 59379 in reply to 59343

    Re: help with ip address regular expression

    Seriously, if you have the option of validating the actual "values" of each part of the IP address in your programming code, then that is the way to go.

    As you have already found out, validation of value ranges (numerical or alphabetic) using a regex pattern is a real pain and is VERY error prone. A basic limitation is that regex patterns are designed to look for sequences of characters but they have no understanding of what the characters represent: as far as a regex engine is concerned the "9" character is simply a character, as is "$" OR "A". While you can create very convoluted patterns to validate IP address ranges, it is MUCH simpler to simply use something like

    (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})

    and then convert the character sequences captured in match groups #1 to #4 into numbers and then do the range checks. This approach also makes "001" and "1" equivalent (which they are NOT at the character level but ARE at the numerical and IP concept levels).

    Susan

View as RSS news feed in XML