2014-05-20 162 views
0

我有这样的正则表达式可以正常工作完美匹配USA格式($ 1.50)的货币值:正则表达式为货币价值

Regex money = new Regex(@"\w\^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$"); 

我想有一定的帮助,以了解如何建立一个这样的正则表达式,我需要更改我的国家的格式,如更改$R $

我期待在MSDN上的主题,但没有到目前为止工作...

回答

1

你的问题有三个部分,并我听起来好像它主要是关于“学习如何钓鱼”,这很棒。

** A。正则表达式你想**

基础上的评论,你正在寻找这个(see demo):

^R\$\d+(?:\.\d{3})*,\d{2}$ 

的正则表达式

的B.说明这是一个相对简单的正则表达式,为此您可以阅读自动生成的解释。有几个网站这样做。这里是one(它会在原始网站上显示得更好)。

NODE      EXPLANATION 
-------------------------------------------------------------------------------- \w      word characters (a-z, A-Z, 0-9, _) 
-------------------------------------------------------------------------------- \^      '^' 
-------------------------------------------------------------------------------- \$      '$' 
-------------------------------------------------------------------------------- (      group and capture to \1: 
-------------------------------------------------------------------------------- 
    \d{1,3}     digits (0-9) (between 1 and 3 times 
          (matching the most amount possible)) 
-------------------------------------------------------------------------------- 
    (      group and capture to \2 (0 or more times 
          (matching the most amount possible)): 
-------------------------------------------------------------------------------- 
     \,      ',' 
-------------------------------------------------------------------------------- 
     \d{3}     digits (0-9) (3 times) 
-------------------------------------------------------------------------------- 
    )*      end of \2 (NOTE: because you are using a 
          quantifier on this capture, only the 
          LAST repetition of the captured pattern 
          will be stored in \2) 
-------------------------------------------------------------------------------- |      OR 
-------------------------------------------------------------------------------- 
    (      group and capture to \3: 
-------------------------------------------------------------------------------- 
     \d+      digits (0-9) (1 or more times 
           (matching the most amount possible)) 
-------------------------------------------------------------------------------- 
    )      end of \3 
-------------------------------------------------------------------------------- )      end of \1 
-------------------------------------------------------------------------------- (      group and capture to \4 (optional 
          (matching the most amount possible)): 
-------------------------------------------------------------------------------- 
    \.      '.' 
-------------------------------------------------------------------------------- 
    \d{2}     digits (0-9) (2 times) 
-------------------------------------------------------------------------------- )?      end of \4 (NOTE: because you are using a 
          quantifier on this capture, only the LAST 
          repetition of the captured pattern will be 
          stored in \4) 
-------------------------------------------------------------------------------- $      before an optional \n, and the end of the 
          string 

C.我怎样才能学会建立一个正则表达式像那样

下面是我推荐的资源。

  1. 书籍:精通正则表达式(第3版),正则表达式食谱

  2. 网站:regular-expressions.info,RexEgg,FAQ on SO

  3. 工具:使用RegexBuddy(商业,但优秀的调试器) ,regex101.com,Debuggex.com

+0

如果我把** R ** *放在** \ $ **之前,并且保留它: *正则表达式金钱= new Regex(@“\ w \^R \ $(\ d {1,3}(\,\ d {3})* |(\ d +))(\。\ d {2})$”) ; *。而我尝试使用* isMatch *的值* R $ 1,50 *它仍然返回false。为什么? – PlayHardGoPro

+0

@PlayHardGoPro你发送了几个错别字。这是更清洁的'^ R \ $(\ d {1,3}(,\ d {2})* |(\ d +))?$':)如果您在regex101.com上使用它,您可以看到结果马上 – zx81

+0

@PlayHardGoPro这里是一个[demo](http://regex101.com/r/nG6sX3)顺便说一句'''后面的部分允许你匹配更多的数字,如果没有逗号,那就是你想要的? – zx81

1

你可以只是在你的正则表达式添加R像这样:

Regex rmoney = new Regex(@"\w\^R\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$"); 
+0

是否有一个教程/文章或可以让我明白如何建立一个?我只是不明白这些斜线括号等...; s – PlayHardGoPro

+0

@PlayHardGoPro,http://regexone.com/ – sshashank124

+0

如果我* * 1,50 * R *作为参数传递给* isMatch *它仍然返回false .. 。 – PlayHardGoPro