whats the regex to validate a comma delimited list like this one 12365, 45236, 458, 1, 99996332, ...... thanks
+1
A:
You might want to specify language just to be safe, but
(\d+, ?)+(\d+)?
ought to work
David Berger
2009-09-08 20:15:08
This solution fails for a list containing only 1 element. See my solution below.
Asaph
2009-09-08 20:41:05
+1
A:
It depends a bit on your exact requirements. I'm assuming: all numbers, any length, numbers cannot have leading zeros nor contain commas or decimal points. individual numbers always separated by a comma then a space, and the last number does NOT have a comma and space after it. Any of these being wrong would simplify the solution.
([1-9][0-9]*,[ ])*[1-9][0-9]*
Here's how I built that mentally:
[0-9] any digit.
[1-9][0-9]* leading non-zero digit followed by any number of digits
[1-9][0-9]*, as above, followed by a comma
[1-9][0-9]*[ ] as above, followed by a space
([1-9][0-9]*[ ])* as above, repeated 0 or more times
([1-9][0-9]*[ ])*[1-9][0-9]* as above, with a final number that doesn't have a comma.
mcherm
2009-09-08 20:16:30
+2
A:
The accepted solution will not work for a list containing only 1 element. Also, it will match trailing comma and whitespace. I suggest it be modified to:
(\d+)(,\s*\d+)*
Asaph
2009-09-08 20:40:06
you are right, i had to strip a first character before I could use the regex, thanks all for helping out
everLearningStudent
2009-09-15 17:54:51
@ondrobaco: You're probably only inspecting the first match group. The next match group will contain the rest of the list.
Asaph
2009-12-28 06:12:28