Reputation: 1263
I have a legacy code base where regex validation is being done like this:
string regexString = null;
string matchingString = null;
// Code to populate this two
if (matchingString.Equals(regex.Match(matchingString).Value))
{
//success code
}
else
{
//failure code
}
I cannot make any change in this code. But I can set the regexString by setting a particular value. Now that you know the situation, here is the problem:
I want to validate a given string for the following conditions:
e.g:
Valid values are: 1
, 27
, 453
, *
.
Invalid values are: 4563
, 5*
, 54*
.
I am using the regular expression string as [0-9]{0,3}|\*
. This works perfect for the numbers but failing miserably for *
.
Surprisingly (for me),[0-9]{3}|\*
validates any three digit number (fixed length) and *. Any help will be appreciated.
Upvotes: 0
Views: 170
Reputation: 50104
The reason your regular expression is failing is because of the way |
works!
When presented with the input *
, the regular expression works as follows:
|
""
It is NOT going "OK, I've matched zero digits, but I'd better check to see if I can match the star instead.
Thus Value
is ""
, and your enclosing code then realises that "" != *
. " and goes down the else
branch.
How to fix this?
*
before the |
and the [0-9]{0-3}
after, @"\*|[0-9]{0,3}"
- this makes it match the star first if it can.{0,3}
with {1,3}
and add a second |
with nothing after, @"[0-9]{1,3}|\*|"
- this makes it clear that you're matching one-to-three digits OR star OR nothing.^
and $
, forcing the regular expression to match the whole string - this is how the regular expression should work but it makes a mockery of the containing code. Which is fair enough, the containing code is pretty terrible.)Upvotes: 1
Reputation: 89547
did you try this syntax:
string regexString = @"^(?:[0-9]{1,3}|\*)$";
or
string regexString = "^(?:[0-9]{1,3}|\\*)$";
Upvotes: 4