[英]Regex for currency value
我有这个正则表达式可以完美匹配美国格式($ 1.50)的货币值:
Regex money = new Regex(@"\w\^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$");
我想对如何建立这样的正则表达式有所帮助,我需要更改国家/地区的格式,例如将$更改为R $ 。
我正在寻找msdn主题,但到目前为止没有任何效果...
您的问题分为三个部分,在我看来,这主要是关于“学习如何钓鱼”的,这很棒。
**一种。 您想要的正则表达式**
根据评论,您正在寻找这个( 请参阅demo ):
^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.如何学习构建这样的正则表达式
这是我推荐的资源。
书籍:掌握正则表达式(第三版),Regex食谱
网站: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})?$");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.