I tested the following regexp for US prices (delimiter comma, separator dot) which is running fine:
^[1-9][0-9]{0,2}(?:,?[0-9]{3}){0,3}(\.[0-9]{2})?$
It works, prices like 30,000.000 are refused. Only 2 decimals are accepted.
I tried to exchange them for handling European prices:
^[1-9][0-9]{0,2}(?:.?[0-9]{3}){0,3}(\,[0-9]{2})?$
but it does not work, prices like 30.000,000 are accepted, which is wrong. Only 2 decimals should be accepted.
What’s wrong in these 2 regexps?
In regex,
.is a special character so when you want to match a literal dot you need to escape it with a backslash (\.). This is not the case for commas, so you can leave them unchanged.In your attempt you switched the
,and the.which left you with an unescaped.and a\,at the end, when you actually want to switch the,and the\.like this: