代码之家  ›  专栏  ›  技术社区  ›  Robert Claypool

什么是“最好的”美国货币正则表达式?

  •  44
  • Robert Claypool  · 技术社区  · 16 年前

    a lot of results .

    我选择其中一个的问题是,如果不测试所有的边缘情况,正则表达式很难验证。有没有人有一个已经过注册的美元正则表达式 ?

    System.Decimal :

    [ws][sign][digits,]digits[.fractional-digits][ws] 
    
    Elements in square brackets ([ and ]) are optional. 
    The following table describes each element. 
    
    ELEMENT             DESCRIPTION
    ws                  Optional white space.
    sign                An optional sign.
    digits              A sequence of digits ranging from 0 to 9.
    ,                   A culture-specific thousands separator symbol.
    .                   A culture-specific decimal point symbol.
    fractional-digits   A sequence of digits ranging from 0 to 9. 
    
    11 回复  |  直到 4 年前
        1
  •  107
  •   Keng    16 年前

    这里有一些来自Regex Buddy制造商的东西。这些来自图书馆,所以我相信它们已经过彻底的测试。

    编号:货币金额(必须为美分) 可选数千个分离器;强制两位数分数

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

    可选数千个分离器;可选两位数分数

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

    编号:货币金额(美元和美元);欧盟(可选) 可以使用美式123456.78符号和欧式123.456.78符号。可选数千个分离器;可选两位数分数

    Match; JGsoft:
    ^[+-]?[0-9]{1,3}(?:[0-9]*(?:[.,][0-9]{2})?|(?:,[0-9]{3})*(?:\.[0-9]{2})?|(?:\.[0-9]{3})*(?:,[0-9]{2})?)$
    
        2
  •  20
  •   Developer    13 年前

    过去几年我一直在成功地使用它。

    "^\$?\-?([1-9]{1}[0-9]{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\-?\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))$|^\(\$?([1-9]{1}\d{0,2}(\,\d{3})*(\.\d{0,2})?|[1-9]{1}\d{0,}(\.\d{0,2})?|0(\.\d{0,2})?|(\.\d{1,2}))\)$"
    
        3
  •  9
  •   PhiLho    16 年前

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

    0
    1
    33
    555
    4,656
    4656
    99,785
    125,944
    7,994,169
    7994169
    0.00
    1.0
    33.78795
    555.12
    4,656.489
    99,785.01
    125,944.100
    -7,994,169
    -7994169.23 // Borderline...
    
    Wrong:
    000
    01
    3,3
    5.
    555,
    ,656
    99,78,5
    1,25,944
    --7,994,169
    0.0,0
    .10
    33.787,95
    4.656.489
    99.785,01
    1-125,944.1
    -7,994E169
    


    在其周围添加空格是很简单的。

        4
  •  3
  •   Leandro Bardelli    10 年前

    Keng的答案是完美的,我只想补充一点,对于使用1或2个小数(对于第三个版本):

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

    净小提琴: https://dotnetfiddle.net/1mUpX2

        5
  •  2
  •   jim    12 年前

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

    $46,48382
    $4,648,382
    $ 4,648,382
    $4,648,382.20
    $4,648,382.2
    $4,6483,82.20
    $46,48382 70.25PD
    $ 46,48382 70.25PD
    
        6
  •  1
  •   Dan L    8 年前

    这个问题已经有几年了,所以我想给出一个更新的答案。

    我用过 jQuery InputMask

    对于货币,我强烈推荐 autoNumeric

    记住 jquery.inputmask 主要是关于控制值的格式,而 autoNumeric

        7
  •  1
  •   abumalick    4 年前

    regex_expression

    /^[-]?[$]\d{1,3}(?:,?\d{3})*\.\d{2}$/ 这就是我想到的-我看到了类似的答案,但我只是用了 \d 而不是 [0-9]

        8
  •  0
  •   eitanpo    13 年前

    我正在使用以下正则表达式进行货币验证:

    ^-?0*(?:\d+(?!,)(?:\.\d{1,2})?|(?:\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?))$
    

    您还可以允许可选的前导美元符号:

    ^\$?-?0*(?:\d+(?!,)(?:\.\d{1,2})?|(?:\d{1,3}(?:,\d{3})*(?:\.\d{1,2})?))$
    

    \( and \)
    
        9
  •  0
  •   NoviceProgrammer    13 年前

    我们可以使用

    CurrencyPositivePattern 
    CurrencyGroupSeparator
    CurrencyDecimalSeparator
    

    特性 NumberFormatInfo

    编辑:类似这样的

    NumberFormatInfo nfi = CultureInfo.CurrentCulture.NumberFormat;
          // Assign needed property values to variables.
          string currencySymbol = nfi.CurrencySymbol;
          bool symbolPrecedesIfPositive = nfi.CurrencyPositivePattern % 2 == 0;
          string groupSeparator = nfi.CurrencyGroupSeparator;
          string decimalSeparator = nfi.CurrencyDecimalSeparator;
    
          // Form regular expression pattern.
          string pattern = Regex.Escape( symbolPrecedesIfPositive ? currencySymbol : "") + 
                           @"\s*[-+]?" + "([0-9]{0,3}(" + groupSeparator + "[0-9]{3})*(" + 
                           Regex.Escape(decimalSeparator) + "[0-9]+)?)" + 
                           (! symbolPrecedesIfPositive ? currencySymbol : ""); 
    

    提及- http://msdn.microsoft.com/en-us/library/hs600312.aspx

        10
  •  0
  •   JoshMahowald    12 年前

    我已经在这方面取得了成功(从上面的一些正则表达式中提取了一些片段)。仅处理多达数千个,但扩展它应该不太困难

    case class CurrencyValue(dollars:Int,cents:Int)
    def cents = """[\.\,]""".r ~> """\d{0,2}""".r ^^ {
      _.toInt
    }
    def dollarAmount: Parser[Int] = """[1-9]{1}[0-9]{0,2}""".r ~ opt( """[\.\,]""".r ~> """\d{3}""".r) ^^ {
      case x ~ Some(y) => x.toInt * 1000 + y.toInt
      case x ~ None => x.toInt
    }
    def usCurrencyParser = """(\$\s*)?""".r ~> dollarAmount ~ opt(cents) <~ opt( """(?i)dollars?""".r) ^^ {
      case d ~ Some(change) => CurrencyValue(d, change)
      case d ~ None => CurrencyValue(d, 0)
    }
    
        11
  •  0
  •   MIkee    8 年前

    这就是我使用的:

    无前导+或-

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

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

    https://jsfiddle.net/compsult/9of63cwk/12/

        12
  •  0
  •   henrywright88404    6 年前

    我用莱安德罗的回答补充道 ^(?:[$]|)

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

    这个匹配

    136,402.99
    25.27
    0.33
    $584.56
    1
    00.2
    3,254,546.00
    $3,254,546.00
    00.01
    -0.25
    +0.85
    +100,052.00
    

    不匹配

    11124.52
    234223425.345
    234.
    .5234
    a
    a.23
    32.a
    a.a
    z548,452.22
    u66.36