Req ex for 9+2 Numerci plus decimal values

Req ex for 9+2 Numerci plus decimal values

我正在寻找具有以下要求的正则表达式:

  1. 小数点后9 + 2
  2. 如果金额为零,应该是无效的

我试过 ^[1-9][0-9]*$ 但确实有效。

对“零”使用负面展望,锚定开始。这是一种方法:

^(?!0*\.00)\d+\.\d\d$

子表达式(?!0*\.00)表示“后面的不能是任意数量的0(包括none)然后.00”。

使用

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

proof

说明

--------------------------------------------------------------------------------
  ^                        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