How validate decimal with optional percentage symbol using Regex?

纵然是瞬间 提交于 2021-02-11 05:28:18

问题


As the title of the question, I need to validate regex using the following values​​: (Maximum 2 decimal places, and 9 integers) with an optional percent symbol.

Valid:

10%
 0%
 1111111.12%
 15.2%
 10
 2.3

Invalid:

 .%
 12.%
 .02%
 %
 123456789123.123

I tryed:

^[0-9]{0,9}([\.][0-9]{0,2})\d[\%]{0,1}?$

But It does not work as I want.


回答1:


try this

^\d{1,9}(\.\d{1,2})?%?$

I tested over rubular and it is ok for your set of example.




回答2:


Try this:

^[0-9]{1,9}([\.][0-9]{1,2})?[\%]?$


来源:https://stackoverflow.com/questions/10560107/how-validate-decimal-with-optional-percentage-symbol-using-regex

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