I’m trying to generate a regular expression with the next pattern.
A number, of a maximum of 16 digits, that can have or not a comma inside, never at the beginning, never at the end.
I tried this:
^(?:\d+(?:,{1}\d+){1})$
The problem is that I can’t count the result of a group {0,16}.
This is a list of numbers that should fit the expression:
123,34
1,33333333
1222222233
Example numbers that shouldn’t fit:
1111,1111,1111
,11111
11111,
11111111111111111111111111111,111111111111111 (more than 16
characters)
If your regex flavour supports lookahead assertions, you can use this:
See it here on Regexr
I removed the superfluous
{1}and made the group with the fraction optional.The negative lookahead assertion
(?!(?:\d{17,}$|[,\d]{18,}$))is checking your length requirement. It fails if it finds 17 or more digits till the end OR 18 or more digits and commas till the end. That I allow multiple commas in the character class here is not a problem, that there is only one comma is ensured by the following pattern.