Reputation: 15
I need a regex expression that will match a comma separated list of integers. I've tried many things, and they work, but they don't work well enough for what I need.
The expression must reject any trailing comma (ex. 1,2,), any double commas (ex 1,1,,23), and any non-numeric character (ex. 1,a,2,43,2).
There must also be no spaces between the numbers. The numbers themselves can be of any length.
An example of a string that should be matched is 1,2,3,4,5,9,234
I appreciate anybody who can help me with this.
Upvotes: 0
Views: 75
Reputation: 10360
Try this regex:
^\d+(?:,\d+)*$
Explanation:
^
- asserts the start of the line\d+
- matches 1+ digits(?:,\d+)*
- matches 0+ occurrences of a comma followed by 1+ digits$
- asserts the end of the lineUpvotes: 2