Simple regular expression for a decimal with a precision of 2

后端 未结 17 2040
闹比i
闹比i 2020-11-22 02:02

What is the regular expression for a decimal with a precision of 2?

Valid examples:

123.12
2
56754
92929292929292.12
0.21
3.1
17条回答
  •  抹茶落季
    2020-11-22 02:36

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

    Will make things like 12. accepted. This is not what is commonly accepted but if in case you need to be “flexible”, that is one way to go. And of course [0-9] can be replaced with \d, but I guess it’s more readable this way.

提交回复
热议问题