RegEx的美国货币格式和长度限制

时间:2012-08-14 14:16:35

标签: regex

我面临着美国货币正则表达式的几个挑战。 对美国货币格式的一点研究使我得到了以下正则表达式,这就像奇迹一样。

^[+-]?[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但是.99不匹配。因此,我将RegEx更新为:^[0-9]{**0**,3}(?:,?[0-9]{3})*(?:\.[0-9]{0,10})?$

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

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

任何帮助都将不胜感激。

3 个答案:

答案 0 :(得分:1)

试试这个:

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

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

答案 1 :(得分: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:
所有修复都包含在内。请检查是否遗漏了。

答案 2 :(得分: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