I'm trying to create a regex that accept: An empty string, a single integer or multiple integers separated by a comma but can have no starting and ending comma.
I managed to find this, but I cannot undertsand how to remove the digit limit
^\d{1,10}([,]\d{10})*$
The thing you posted still requires at least 1 integer, so it won't match an empty string:
Here is what you need:
^(\d+(,\d+)*)?$
Explaination:
'?'
so as to match the empty string.'\d+'.
That is 1 or more digit characters ('0'-'9')
',\d+'
and put an asterisk after it.Hench the whole thing is either an empty string or start with an integer then repeat zero or more times a string which starts with a comma and ends with an integer
{1,10}
and {10}
are ranges. You can replace them with +
for infinite-positive. Eg.:
^\d+([,]\d+)*$
Try the following:
^(\d+(,\d+)*)?$
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With