【问题标题】:ASP.NET Currency regular expressionASP.NET 货币正则表达式
【发布时间】:2015-04-28 01:39:39
【问题描述】:

所需的货币格式如下所示

1,100,258
100,258
23,258
3,258

或所有整数,如 1234562421323 等等。

我在下面输入ValidationExpression

(^[0-9]{1,3}(,\d{3})*) | (^[0-9][0-9]*)

但它不起作用。

【问题讨论】:

    标签: asp.net regex validation webforms


    【解决方案1】:

    你有ignore pattern whitespace 吗?如果没有,请删除管道两侧的两个空格。

    由于您尝试匹配任何一个,因此您应该在字符串末尾添加一个标记 $,就像这样

    还有^[0-9][0-9]*有什么意义,什么时候可以用^[0-9]+

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

    ^(\d{1,3}(?:,\d{3})*|\d+)$
    

    解释:

     ^                     # Anchors to the beginning to the string.
     (                     # Opens CG1
         \d{1,3}           # Token: \d (digit)
         (?:               # Opens NCG
             ,             # Literal ,
             \d{3}         # Token: \d (digit)
                             # Repeats 3 times.
         )*                # Closes NCG
                             # * repeats zero or more times
     |                     # Alternation (CG1)
         \d+               # Token: \d (digit)
                             # + repeats one or more times
     )                     # Closes CG1
     $                     # Anchors to the end to the string.
    

    【讨论】:

    • 很高兴它成功了,谢谢。您也可以投票给答案:)。当然,您可以随心所欲地投票,而您只能接受一个作为“答案”。 @CodaChang
    猜你喜欢
    • 2012-11-30
    • 2010-10-23
    • 1970-01-01
    • 1970-01-01
    • 2015-02-26
    • 1970-01-01
    • 1970-01-01
    • 2022-11-26
    相关资源
    最近更新 更多