-1

I have a problem with regex and need your help. I want to check my string is correct or incorrect. First and last is a number, only number and comma after it. No space inside 2 numbers.

Ex:

  • 1,2,3,49,5 this is correct
  • 1,2,3,45, this is incorrect
  • ,12,4,2,67 this is incorrect
  • 1,2 3,4,5,6 this is incorrect
Uwe Keim
  • 39,551
  • 56
  • 175
  • 291

2 Answers2

0

Please check below regex to solve your problem.

Regex: ^[0-9]+([0-9,])+[0-9]+$

^[0-9]+ is for start with one or more number

[0-9]+$ is for end with one or more number

([0-9,])+ is for one or more number with comma

Please check the output in Regex101

Update:

Please check the updated regex: ^(\d+,)+\d+$

^(\d+,)+ is for one or more number with comma and this will handle first number with comma

\d+$ is for end with one or more number

Please check the updated output in Regex101

csharpbd
  • 3,786
  • 4
  • 23
  • 32
0
^(?:\d+,)*\d+$

(?:\d+,)* - gets matches like "0," "00," "000," ... or empty

\d+ - gets last number as "0" "00" "000"

Eduard
  • 475
  • 4
  • 14