问题
I have a regular expression that needs to validate if the text is in a supported currency format.
^(\$|€|Fr.|£|kr|R?)\s*((([1-9](,\d{3}){3})|([1-9]\d{0,2}(,\d{3}){0,2})|(\d{1,10}))(\.\d{1,2})?)\s*(\$|€|Fr.|£|kr|R?)$
I support the following:
US Dollar (10000.00 or 10,000.00 or $10,000.00)
Euro (10000.00 or 10,000.00 or €10,000.00)
Francs (10000.00 or 10'000.00 or Fr.10'000.00)
Pounds (10000.00 or 10'000.00 or £10,000.00)
Kroner (10000,00 or 10'000.00 or 10.000,00 kr)
Rand (10000.00 or 10,000.00 or R10,000.00)
I updated it to support all of the above formats.
^(\$|€|Fr.|£|kr|R?)\s*(((([1-9](,\d{3}){3})|([1-9]\d{0,2}(,\d{3}){0,2})|(\d{1,10}))(\.\d{1,2})?)|((([1-9](.\d{3}){3})|([1-9]\d{0,2}(.\d{3}){0,2})|(\d{1,10}))(\,\d{1,2})?)|((([1-9]('\d{3}){3})|([1-9]\d{0,2}('\d{3}){0,2})|(\d{1,10}))(\.\d{1,2})?))\s*(\$|€|Fr.|£|kr|R?)$
It looks fine, but I don't know why it also accepts 3 decimal place when I have these formats.
100,000.012
200.000,002
100'000.001
I should only accept 2 decimal places.
回答1:
I think you need to remove the ? in \d{1,2})?
来源:https://stackoverflow.com/questions/28883580/regular-expression-that-supports-multiple-currency-formats