C# 货币正则表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1028221/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 05:53:17  来源:igfitidea点击:

Regex for Money

c#asp.net.netregexcurrency

提问by abatishchev

I have asp:TextBoxto keep a value of money, i.e. '1000', '1000,0' and '1000,00' (comma is the delimiter because of Russian standard).

我必须asp:TextBox保持货币价值,即“1000”、“1000,0”和“1000,00”(由于俄罗斯标准,逗号是分隔符)。

What ValidationExpressionhave I to use into appropriate asp:RegularExpressionValidator?

ValidationExpression有什么用成合适的asp:RegularExpressionValidator

I tried \d+\,\d{0,2}but it doesn't allows a number without decimal digits, e.g. just '1000'.

我试过了,\d+\,\d{0,2}但它不允许没有十进制数字的数字,例如只有“1000”。

采纳答案by balpha

\d+(,\d{1,2})?

will allow the comma only when you have decimal digits, and allow no comma at all. The question mark means the same as {0,1}, so after the \d+you have either zero instances (i.e. nothing) or one instance of

仅当您有十进制数字时才允许使用逗号,并且根本不允许使用逗号。问号的含义与 相同{0,1},因此在\d+您有零个实例(即没有)或一个实例之后

,\d{1,2}

As Helen points out correctly, it will suffice to use a non-capturing group, as in

正如海伦正确指出的那样,使用非捕获组就足够了,如

\d+(?:,\d{1,2})?

The additional ?:means that the parentheses are only meant to group the ,\d{1,2}part for use by the question mark, but that there is no need to remember what was matched within these parenthesis. Since this means less work for the regex enginge, you get a performance boost.

附加?:意味着括号仅用于将,\d{1,2}部分分组以供问号使用,但无需记住这些括号内匹配的内容。由于这意味着正则表达式引擎的工作量减少,因此您可以获得性能提升。

回答by Jade Ohlhauser

We use this very liberal regular expression for money validation:

我们使用这个非常自由的正则表达式进行货币验证:

new Regex(@"^\-?\(?$?\s*\-?\s*\(?(((\d{1,3}((\,\d{3})*|\d*))?(\.\d{1,4})?)|((\d{1,3}((\,\d{3})*|\d*))(\.\d{0,4})?))\)?$");

It allows all these: $0, 0, (0.0000), .1, .01, .0001, $.1, $.01, $.0001, ($.1), ($.01), $(.0001), 0.1, 0.01, 0.0001, 1., 1111., 1,111., 1, 1.00, 1,000.00, $1, $1.00, $1,000.00, $ 1.0000, $ 1.0000, $ 1,000.0000, -1, -1.00, -1,000.00, -$1, -$1.00, -$1,000.00, -$ 1, -$ 1.00, -$ 1,000.00, $-1, $-1.00, $-1,000.00, $(1), $(1.00), $(1,000.00), $ (1), $ (1.00), $ (1,000.00), ($1), ($1.00), ($1,000.00)

它允许所有这些:$0, 0, (0.0000), .1, .01, .0001, $.1, $.01, $.0001, ($.1), ($.01), $(.0001 ), 0.1, 0.01, 0.0001, 1., 1111., 1,111., 1, 1.00, 1,000.00, $1, $1.00, $1,000.00, $ 1.0000, $ 1.00,00,10.0,1.00,0.0,1.0,0.0,1.00,0. -$1.00, -$1,000.00, -$ 1, -$ 1.00, -$ 1,000.00, $-1, $-1.00, $-1,000.00, $(1), $(1.00), $(1,000.00), $(1), $ (1.00), $ (1,000.00), ($1), ($1.00), ($1,000.00)

回答by Mike Graf

i used this one in javascript: may be of use for you in c#

我在javascript中使用过这个:在c#中可能对你有用

var entered = '10.00';
var regex = /^\d+(?:\.\d{2})?$/; // starts with N digits optional ".\d\d"
console.log(entered.match(regex));