c# - Regex for integers delimited by commas -


i need regex allows integers (positive , negative) delimited comma 2,-3,4, comma should in middle of 2 integers, not @ start or end or 2 consecutive commas 23,34,,4.

currently have this:

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

but doesn't seems match thing rejects valid inputs 123,34,2,34,234

can please point out wrong above regex.

the \d{10} subpattern matches 10-digit chunks.

you need allow 1 10 {1,10} (or 1 , more +) with

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

or

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

note use of non-capturing group (?:...) not store submatches, , meant group sequence of subpatterns.

see regex demo

edit: allow matching negative values, add optional -:

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

see another regex demo.


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -