我有这个正则表达式可以完美匹配美国格式的货币值(1.50 美元):
Regex money = new Regex(@"\w\^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");
我想对了解如何构建这样的正则表达式有所帮助,我需要更改我所在国家/地区的格式,例如将$更改为R$。
我正在寻找 msdn 主题,但到目前为止没有任何效果......
我有这个正则表达式可以完美匹配美国格式的货币值(1.50 美元):
Regex money = new Regex(@"\w\^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");
我想对了解如何构建这样的正则表达式有所帮助,我需要更改我所在国家/地区的格式,例如将$更改为R$。
我正在寻找 msdn 主题,但到目前为止没有任何效果......
您的问题分为三个部分,对我来说,这听起来主要是关于“学习如何钓鱼”,这很好。
**一种。你想要的正则表达式**
根据评论,您正在寻找这个(见演示):
^R\$\d+(?:\.\d{3})*,\d{2}$
B. 正则表达式的解释
这是一个相对简单的正则表达式,为此您可以阅读自动生成的解释。有几个网站这样做。这是一个(它将在原始站点上显示得更好)。
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. 我怎样才能学会构建这样的正则表达式
以下是我推荐的资源。
书籍:掌握正则表达式(第 3 版)、Regex Cookbook
网站:regular-expressions.info、RexEgg、SO 常见问题解答
工具:RegexBuddy(商业但出色的调试器)、regex101.com、Debuggex.com
您可以R
像这样在您的正则表达式中添加一个:
Regex rmoney = new Regex(@"\w\^R\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");