beyonddc
beyonddc

Reputation: 1256

Regular Expression, comma must follow 1 or 2 digits numeric

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

Answers (3)

Bohemian
Bohemian

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

Ian Kenney
Ian Kenney

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

Elmar Peise
Elmar Peise

Reputation: 15413

\d{0,2} means "between 0 and 2 digits". It should be \d{1,2}

Upvotes: 6

Related Questions