peroija
peroija

Reputation: 1982

match these values except for zero

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

Answers (3)

David Brabant
David Brabant

Reputation: 43559

^([1-9][0-9]?)|100|888|777|999$

Upvotes: 1

Dan
Dan

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

Frank Schmitt
Frank Schmitt

Reputation: 30835

This should achieve what you want:

^(([1-9][0-9]?)|100|888|777|999)$

Upvotes: 1

Related Questions