user7247929
user7247929

Reputation: 33

Regex to match two or more comma separated integers

I have a list of comma separated values:

123            should fail   // using my regex this pass 
123, 230       should pass
234, 560, 890  should pass

using this regex ^(\d+(, \d+)*)?$ if it is a single value, it still passes.

How can I only match 2 or more integers in the list?

Upvotes: 3

Views: 114

Answers (2)

Dekel
Dekel

Reputation: 62546

You should use the + instead of * to make sure teh (, \d+) part exists at least 1 time.

^(\d+(, \d+)+)?

Check this:
https://regex101.com/r/yvWiZ0/1

Upvotes: 1

Tim
Tim

Reputation: 3725

Change the * to a +. * means 0 or more matches, + means 1 or more.

Upvotes: 4

Related Questions