views:

190

answers:

5

Hey guys

I'm trying to use a RegularexpressionValidator to match an IP address (with possible wildcards) for an IP filtering system.

I'm using the following Regex:

"([0-9]{1,3}\\.|\\*\\.){3}([0-9]{1,3}|\\*){1}"

Which works fine when running it in LINQPad with Regex.Matches, but doesn't seem to work when I'm using the validator.

Does anyone have a suggestion as to either a better Regex or why it would work in test but not in situ?

Cheers, Ed

+1  A: 

How about putting start and end string characters on the expression

^([0-9]{1,3}\\.|\\*\\.){3}([0-9]{1,3}|\\*){1}$
Mike
Nice suggestion but I'm afraid that still doesn't validate!
Ed Woodcock
+1  A: 

[0-9]{1,3} would allow IP addresses of the form 999.999.999.999 . Your IP address range should allow only 0-255.
Replace all occurences of [0-9]{1,3} with ([0-9]{1,2})|(1[0-9]{2,2})|(2[0-4][0-9])|(25[0-5])
This does seem very complicated to me, and probably there are better ways of doing this, but it seems correct at first glance.

apoorv020
I'm not massively fussed about the actual IP address being valid, only the format (it's easier to parse later), but point taken.
Ed Woodcock
+3  A: 

This: \\.|\\*\\. looks like the dodgy bit. Do this instead:

@"^(([0-9]{1,3}|\*)\.){3}([0-9]{1,3}|\*)$"

And to only accept 0-255 (thanks, apoorv020):

^((([0-9]{1,2})|(1[0-9]{2,2})|(2[0-4][0-9])|(25[0-5])|\*)\.){3}(([0-9]{1,2})|(1[0-9]{2,2})|(2[0-4][0-9])|(25[0-5])|\*)$
Callum Rogers
Another good suggestion, but I'm afraid that doesn't work either!
Ed Woodcock
It does - try it at http://gskinner.com/RegExr/ . Make sure you have an `@` in front of the string. If it still doesn't work, show us the code you are using to match it. Note i'm trying to match it to things like `192.168.*.* and `10.*.*.*` - is this what you want? Can you show us some test cases?
Callum Rogers
@Callum I don't think you read my question quite right: My original Regex will match an IPv4 address with wildcards no worries, but it won't work in an asp:RegularExpressionValidator. Your Regex has exactly the same issue.
Ed Woodcock
Have you tried your original regex, but replacing the escaped backslash `\\` with a regular one `\` (without using the C# `@` escape character)? (i.e. try `^(([0-9]{1,3}|\*)\.){3}([0-9]{1,3}|\*)$` or `(([0-9]{1,3}|\*)\.){3}([0-9]{1,3}|\*)`)
VeeArr
@VeeArr we have a winner. Always forget you don't have to escape in the Regex validators! Cheers, you should put that as a seperate answer so I can accept it.
Ed Woodcock
+2  A: 

asp:RegularExpressionValidator does not require you to double-escape backslashes. You should try:

([0-9]{1,3}\.|\*\.){3}([0-9]{1,3}|\*){1}

VeeArr
Cheers mate! :)
Ed Woodcock
A: 

My answer is general for .NET, not RegularExpressionValidator-specific.

Regex string for IP matching (use ExplicitCapture to avoid useless capturing and keep RE concise):

"\\b0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})(\\.0*(2(5[0-5]|[0-4]\\d)|1?\\d{1,2})){3}\\b"

Depending on particular use case you may want to add appropriate anchors, i.e. \A or ^ at the beginning and \Z or $ at the end. Then you can remove word-boundaries requirement: \b.

(Remember about doubling \ inside the string)

przemoc