Skip to content
Advertisement

Regular Expressions to Match Decimal 12,2 With Comma to Separate Groups of Thousands

I want to make sure that the input is no more than 10 digits before the decimals point so it will match DECIMAL(12,2).

I already tried :

^(?=.{0,13}$)[0-9]{1,3}(,[0-9]{3})*(.[0-9]+)?$

But it counts the digits after decimals too, so what I want to match is:

1,111,111,111.00

so it will keep matching with or without the decimals, what I tried only can match with:

1,111,111,111

Advertisement

Answer

You can assert from the start of the string what is on the right is 1-10 times a digit followed by an optional comma followed by a dot and 2 digits.

Then use the pattern to match the exact format.

Note that in your pattern the (.[0-9]+)? at the end can match more than 2 digits. If you want to match an optional dot and 2 digits, use (?:.[0-9]{2})?

^(?=(?:d,?){1,10}(?:.d{2})?$)[0-9]{1,3}(?:,[0-9]{3})*(?:.[0-9]+)?$

Regex demo

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement