Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex to match comma separated list with no comma on the end

Tags:

c#

.net

regex

I need a .Net (C#) Regex to match a comma separated list of numbers that will not match if there is a comma as the last character

123,123,123,123 true - correct match
123,123,123,123, false - comma on end 
123,123,123,,123 false - double comma
,123,123,123,123 false - comma at start
"" false - empty string

123 true - single value

I have found this Regex but matches when there is a comma on the end ^([0-9]+,?)+$

What would be a Regex pattern that would fit this pattern?

EDIT: Added 1 example for clarity the correct answer works for 123

like image 983
ojhawkins Avatar asked Jul 19 '13 03:07

ojhawkins


1 Answers

Try using this pattern:

^([0-9]+,)*[0-9]+$

You can test it here.

like image 96
p.s.w.g Avatar answered Oct 22 '22 19:10

p.s.w.g