tags:

views:

76

answers:

5

The possible values are...

1 (it will always start with a number)
1,2
4,6,10
+2  A: 

This should do it:

(\d+,?)+
phalacee
If it has to match at least one number, that should be a + rather than *
Tom Smilack
100% correct smialck, I completely missed that. Thank you
phalacee
In other way this same as a string of numbers contain comma not consecutive. Then first + can be omitted `(\d+,?)+` -> `(\d,?)+`
pinichi
@pinichi: I don't understand what you mean
phalacee
I mean that can be more shorter, Ex: "11,2" can be express as [(1)(1)(,)](2) in `(\d+,?)+` case and as (1)(1,)(2) in `(\d,?)+` also.
pinichi
(\d,?)+ will only match single digit numbers (0,1,2,3,4,5,6,7,8,9) -- It won't match double-or-greater digit numbers: 10,12,23,100,45 my answer was "(one or more digits optionally followed by comma) one or more times"
phalacee
It will match multiple digit numbers - "10," would be parsed as (1)(0,) - but i will also match a trailing comma
justintime
+1  A: 

This will do:

-?[0-9]+(,-?[0-9]+)*

Or, if you want to be pedantic and disallow numbers starting with 0 (other than 0 itself):

(0|-?[1-9][0-9]*)(,(0|-?[1-9][0-9]*))+

Floating-point numbers are left as an exercise to the reader.

Adam Rosenfield
A: 

Dear Sir, I concure with phalacee
" side note:" would any one like to offer the solution for the thousand million -... format This would be

(\d\d?\d?)*(\d\d\d)\\.(\*\d)

a set of quoted numbers for the case of natural number format like in english locale

"(\d\d?\d?)*(\d\d\d)\\.(\*\d)"*(,"(\d\d?\d?)*(\d\d\d)\\.(\*\d)")

Please correct me appropreatly

William Coleman
You need to ask a separate question rather than posting an answer.
Bill the Lizard
+2  A: 

You can try something like this:

^[0-9]+(,[0-9]+)*
Aragorn
A: 

You'll want

(?<=(?:,|^))\d+(?=(?:$|,))

Regex Buddy explains it as...

Assert that the regex below can be matched, with the match ending at this position (positive lookbehind) «(?<=(?:,|^))»

Match the regular expression below «(?:,|^)»

  Match either the regular expression below (attempting the next alternative only if this one fails) «,»

     Match the character "," literally «,»

  Or match regular expression number 2 below (the entire group fails if this one fails to match) «^»

     Assert position at the start of the string «^»

Match a single digit 0..9 «\d+»

Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»

Assert that the regex below can be matched, starting at this position (positive lookahead) «(?=(?:$|,))»

Match the regular expression below «(?:$|,)»

  Match either the regular expression below (attempting the next alternative only if this one fails) «$»

     Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

  Or match regular expression number 2 below (the entire group fails if this one fails to match) «,»

     Match the character "," literally «,»

I would explain it as, "match any string of digits confirming that before it comes either the start of the string or a comma and that after it comes either the end of the string or a comma". nothing else.

The important thing is to use non-capturing groups (?:) instead of simply () to help overall performance.

Laramie