0

对于美国货币的正则表达式,我面临着一些挑战。对美国货币格式的快速研究使我想到了以下正则表达式,这很奇怪。

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

根据我的其他要求,我已将其修改为以下。因此,下一个成功地将小数点 (.) 后的数字限制为 10。但是,我还有更多挑战。

^[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{0,**10**})?$
  • 我希望我的 RegEx 也能验证 0.99 和 .99。在上面的 RegEx 中,0.99 被验证但 0.99 不匹配。因此,我将 RegEx 更新为:^[0-9]{**0**,3}(?:,?[0-9]{3})*(?:\.[0-9]{0,10})?$

这解决了我当前的问题,但现在它也匹配 ,333,323 模式。

  • 我已更新 RegEx 以将其限制为 10 个小数,但我还要求将其限制为仅小数点前 18 位(没有美国货币分隔符“,”)。我尝试了一些模式,但没有奏效。

任何帮助,将不胜感激。

4

3 回答 3

1

试试这个:

^\d{,3}(?:(?:[^\b],)?\d{3}){,5}(?:\.\d{,10})?$

它解决了您提到的问题,但是这个正则表达式仍然存在一些问题。例如,它允许 99,999999.55 和 0,123.45

于 2012-08-14T14:46:47.780 回答
0

这解决了我当前的问题,但现在它也匹配 ,333,323 模式。
针对此要求修改了 RegEx:

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

但我还要求将其限制为仅小数点前 18 位(没有美国货币分隔符“,”)
我假设您仍然需要逗号分隔符但希望将小数点前的位数限制为 18 不包括逗号. 这意味着您可以拥有 0 到 5 组“,ddd”。如果这个假设是错误的,请告诉我。针对此要求修改了 RegEx:

^((([1-9][0-9]{0,2}(?:,[0-9]{3}){0,5})?)|0|([1-9][0-9]{0,17}))(?:\.[0-9]{1,10})?$

更新 1:
如@davidrac 所述,将正则表达式编辑为不匹配 99,999999.55。

更新 2:
编辑正则表达式以删除前导零

更新 3:
合并了所有修复程序。请检查是否还有更多遗漏。

于 2012-08-14T14:38:12.467 回答
0

怎么样:

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

解释:

The regular expression:

(?-imsx:^(?:[0-9]{1,3}(?:,?[0-9]{3})*)?(?:\.[0-9]{0,10})?$)

matches as follows:

NODE                     EXPLANATION
----------------------------------------------------------------------
(?-imsx:                 group, but do not capture (case-sensitive)
                         (with ^ and $ matching normally) (with . not
                         matching \n) (matching whitespace and #
                         normally):
----------------------------------------------------------------------
  ^                        the beginning of the string
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    [0-9]{1,3}               any character of: '0' to '9' (between 1
                             and 3 times (matching the most amount
                             possible))
----------------------------------------------------------------------
    (?:                      group, but do not capture (0 or more
                             times (matching the most amount
                             possible)):
----------------------------------------------------------------------
      ,?                       ',' (optional (matching the most
                               amount possible))
----------------------------------------------------------------------
      [0-9]{3}                 any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
    )*                       end of grouping
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
----------------------------------------------------------------------
    \.                       '.'
----------------------------------------------------------------------
    [0-9]{0,10}              any character of: '0' to '9' (between 0
                             and 10 times (matching the most amount
                             possible))
----------------------------------------------------------------------
  )?                       end of grouping
----------------------------------------------------------------------
  $                        before an optional \n, and the end of the
                           string
----------------------------------------------------------------------
)                        end of grouping
----------------------------------------------------------------------

一些例子:

match : 1,234.45
match : .123
match : 0.12345
match : 123456.7890123
not match : ,123,456.789
于 2012-08-14T15:03:52.760 回答