I have to make a regex expression to invalidate numbers which are separated by comma.
1_2_3 - valid
1_2_3,1_3 - invalid
What I have so far: ([1-9 _]*[1-9]*[^,]
CodePudding user response:
You can use
^\d (?:_\d )*$
See the regex demo. Details:
^
- start of string\d
- one or more digits(?:
- start of a non-capturing group:_
- an underscore\d
)*
- zero or more occurrences of the pattern sequences in the group$
- end of string.
Note: in case the flavor is JavaScript, it is fine to use \d
to match any ASCII digit and it is equal to [0-9]
. If you need to match any number without leading zeros still accepting a 0
, you may replace each \d
with (?:0|[1-9]\d*)
construct that matches a 0
or a digit other than 0
followed with any zero or more digits.