我的这个正则表达式非常适合匹配U.S.A格式的货币值($ 1.50):
Regex money = new Regex(@"\w\^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");
我想帮助理解如何构建这样的正则表达式,我需要更改我的国家/地区的格式,例如更改 $ R $ 。
我正在寻找msdn主题但到目前为止没有任何工作......
答案 0 :(得分:2)
你的问题有三个部分,对我而言,听起来主要是关于“学习如何捕鱼”,这很棒。
** 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
<强>℃。我怎样才能学会构建像这样的正则表达式
以下是我推荐的资源。
书籍:掌握正则表达式(第3版),Regex Cookbook
网站:regular-expressions.info,RexEgg,FAQ on SO
工具:RegexBuddy(商业但出色的调试器),regex101.com,Debuggex.com
答案 1 :(得分:1)
您只需在正则表达式中添加R
即可:
Regex rmoney = new Regex(@"\w\^R\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");