Reputation: 1982
i have the following regex expression ^(([0-9]{1,2})|100|888|777|999)$
this allows someone to enter the digits: 0-99, 100, 888, 777, 999
I now have to alter the expression to allow: 1-99, 100, 888, 777, 999 (essentially ZERO is not allowed now)
I thought of doing ^(!0|([0-9]{1,2})|100|888|777|999)$
... but this of course won't work because that would be like saying "if it isn't zero, we are fine"
Any advice?
Upvotes: 0
Views: 87
Reputation: 10786
Try splitting off the cases for one digit vs two digit numbers:
^([0-9]|([1-9][0-9])|100|888|777|999)$
And then remove 0:
^([1-9]|([1-9][0-9])|100|888|777|999)$
Now that you have that, there's an obvious way to simplify it:
^(([1-9][0-9]?)|100|888|777|999)$
So, twain249, if you want to read in numbers with excessive zeroes at the front, but you won't want the match to include them:
^0*(([1-9][0-9]?)|100|888|777|999)$
Upvotes: 5
Reputation: 30835
This should achieve what you want:
^(([1-9][0-9]?)|100|888|777|999)$
Upvotes: 1