Regex 2 digits separated by commas, not all
I need a regular expression for the following input:
[2 digits], comma, [two digits], comma, [two digits]
2 digits cannot start with 0. Only the first 2 digits are allowed. Or enter the first 2 digits, then a comma, then the next 2 digits. Or enter the full line as described above.
Valid input:
10
99
17.56
15.99
10.57.61
32.44.99
Can anyone help me with this regex?
At the moment I have this regex, but it does not limit the input to a maximum of 3 groups of 2 digits:
^\d{2}(?:[,]\d{2})*$
source to share
^[1-9]\d(?:,[1-9]\d){0,2}$
The first part ( [1-9]\d
) is just the first number that should be present at all times. It consists of a non-zero digit and an arbitrary second digit ( \d
).
What follows is a non-capturing group ( (?:...)
) containing a comma followed by another two-digit number ( ,[1-9]\d
), just like the first. This group can be repeated between 0 and 2 times ( {0,2}
), so you get either not, or one or two comma sequences and another number.
You can easily expand the part in curly braces to allow for more allowed numbers.
source to share