tags:

views:

38

answers:

2

Hi,

i need a regex for: 123,456,789,123,4444,... basically comma separated values. The INT part can be 1-4 numbers long, followed by a comma...always in this form...

/^([0-9]{1,4})(\,)?$/

This obviously doesn't work...

Thanks!

+2  A: 

Try this:

/^[0-9]{1,4}(?:,[0-9]{1,4})*$/

This will match any comma separated sequence of one or more digit sequences with one to four digits. (?:…) is a so called non-capturing group that’s match cannot be referenced separately like you can with “normal” capturing groups (…).

Gumbo
+3  A: 

Try this:

/^\d{1,4}(?:,\d{1,4})*+$/D

This will match any comma-separated sequence of one or more digit sequences with one to four digits. The D modifier makes sure that any trailing newline character does not mistakenly result in a positive match.

salathe
Please make it less obvious that you copied my answer.
Gumbo
What's with the `*+`? Is that a typo, or a construct that I'm unaware of?
Ryan Kinal
@Gumbo, no. Why *hide* that I copied your answer?
salathe
@Ryan Kinal, it's a [possessive quantifier](http://www.regular-expressions.info/possessive.html).
salathe