我正在寻找具有以下要求的正则表达式:
- 小数点后 9 + 2
- 如果金额为零,它应该是无效的
我试过^[1-9][0-9]*$
了,但它确实有效。
利用
^(?![0.]+$)\d{1,9}\.\d{2}$
见证明
解释
--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
[0.]+ any character of: '0', '.' (1 or more
times (matching the most amount
possible))
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
\d{1,9} digits (0-9) (between 1 and 9 times
(matching the most amount possible))
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d{2} digits (0-9) (2 times)
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string
对“零”使用负面展望,锚定开始。这是一种方法:
^(?!0*\.00)\d+\.\d\d$
子表达式的(?!0*\.00)
意思是“后面的内容不能是任何数量的0
's (包括没有) then .00
”。