货币正则表达式与数字范围匹配

时间:2012-08-08 13:43:54

标签: php regex

我正在使用这个正则表达式:

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

这个正则表达式匹配美元货币金额与逗号或没有

我希望使用货币格式匹配1000到2000之间的数字。

示例:

匹配$1,500.00 $2000.0 $1100.20 $1000

不匹配$1,000.0000 $3,000 $2000.1 $4,000 $2500.50

2 个答案:

答案 0 :(得分:1)

[$]\([1][[:digit:]]\{3\}\|2000\)\(,[[:digit:]]+\|\)

此表达式定义了这种语言:

  • 美元后跟1 xyz或2000。

  • 最后,可选择使用逗号和一个或多个数字

答案 1 :(得分:1)

怎么样:

/^\$(?:1,?\d{3}(?:\.\d\d?)?|2000(?:\.00?)?)$/

<强>解释

 ^                        the beginning of the string
----------------------------------------------------------------------
  \$                       '$'
----------------------------------------------------------------------
  (?:                      group, but do not capture:
----------------------------------------------------------------------
    1                        '1'
----------------------------------------------------------------------
    ,?                       ',' (optional (matching the most amount
                             possible))
----------------------------------------------------------------------
    \d{3}                    digits (0-9) (3 times)
----------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------
      \.                       '.'
----------------------------------------------------------------------
      \d                       digits (0-9)
----------------------------------------------------------------------
      \d?                      digits (0-9) (optional (matching the
                               most amount possible))
----------------------------------------------------------------------
    )?                       end of grouping
----------------------------------------------------------------------
   |                        OR
----------------------------------------------------------------------
    2000                     '2000'
----------------------------------------------------------------------
    (?:                      group, but do not capture (optional
                             (matching the most amount possible)):
----------------------------------------------------------------------
      \.                       '.'
----------------------------------------------------------------------
      0                        '0'
----------------------------------------------------------------------
      0?                       '0' (optional (matching the most
                               amount possible))
----------------------------------------------------------------------
    )?                       end of grouping
----------------------------------------------------------------------
  )                        end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping