Regex for comma separated integers

I need Regexone that only allows integers (positive and negative) separated by a comma 2,-3,4, but the comma should only be in the middle of two integers, not at the beginning or end or two consecutive commas such as 23,34,,4.

I currently have this:

Regex regex = new Regex(@"^\d{1,10}([,]\d{10})*$");
 if (!regex.IsMatch("123,34,2,34,234"))

But it does not seem to match a single thing that even rejects valid inputs, such as 123,34,2,34,234

Could you please indicate what is wrong with my previous regular expression.

+4
source share
1 answer

The subpattern \d{10}corresponds to only 10-digit fragments.

1 10 {1,10} ( 1 +)

@"^\d{1,10}(?:,\d{1,10})*$"

@"^\d+(?:,\d+)*$"

- (?:...), , .

regex

EDIT: , -:

@"^-?\d+(?:,-?\d+)*$"
   ^^       ^^  

regex.

+5

Source: https://habr.com/ru/post/1660383/


All Articles