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