Regular Expression that supports multiple currency formats

…衆ロ難τιáo~ 提交于 2019-12-25 01:55:51

问题


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:

  1. US Dollar (10000.00 or 10,000.00 or $10,000.00)
  2. Euro (10000.00 or 10,000.00 or €10,000.00)
  3. Francs (10000.00 or 10'000.00 or Fr.10'000.00)
  4. Pounds (10000.00 or 10'000.00 or £10,000.00)
  5. Kroner (10000,00 or 10'000.00 or 10.000,00 kr)
  6. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!