Reputation: 1256
I am trying to write a regular expression that ensure if there's a comma then the following text should be 1 or 2 digits numeric.
Here's what I have so far.
(^\d{0,2})+(,\d{0,2})*$
The works in most cases but it is considering the following as valid.
12,22,,,,,,,,,, and 12,22,,,,,,,,,,12,12
What did I do wrong? Thanks!
Upvotes: 1
Views: 961
Reputation: 424993
Use a negative look-ahead to assert that there aren't 3 digits after a comma, and keep the main regex simply "all commas or digits"
^(?!.*,\d{3})[,\d]+$
Upvotes: 3
Reputation: 6426
You are matching 0 to 2 digits after the comma instead of 1 or 2 the following should do the trick
(^\d{1,2})+(,\d{1,2})*$
Upvotes: 4