什么是可以验证货币、浮点数或整数的 C# 正则表达式?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/617826/
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-04 10:26:25  来源:igfitidea点击:

What's a C# regular expression that'll validate currency, float or integer?

c#regexnumberscurrency

提问by nailitdown

What is a regular expression suitable for C# that'll validate a number if it matches the following?

什么是适合 C# 的正则表达式,如果它与以下匹配,它将验证一个数字?

 ,000,000.150
 000000.199
 000 
 1,000,000.150
 100000.123
 10000
 ,000,000.150
 000000.199
 000 
 1,000,000.150
 100000.123
 10000

Or the negative equivalents?

还是负等价物?

采纳答案by gregwhitaker

You can use csmba's regex if you make one slight modification to it.

如果对 csmba 的正则表达式稍作修改,则可以使用它。

^$?(\d{1,3},?(\d{3},?)*\d{3}(.\d{0,3})?|\d{1,3}(.\d{2})?)$

回答by csmba

^$?(\d{1,3},?(\d{3},?)*\d{3}(\.\d{1,3})?|\d{1,3}(\.\d{2})?)$

回答by James

Try this one. It may need some fine tuning to only allow for a single decimal point, but it does match your test cases. I hope this helps.

试试这个。它可能需要一些微调以只允许一个小数点,但它确实与您的测试用例相匹配。我希望这有帮助。

[$\d,.]+

回答by Renaud Bompuis

I think ssg is right. It's not a really good use of Regex, especially if your software has to deal with non-US centric data entry.

我觉得ssg是对的。这不是正则表达式的真正用途,特别是如果您的软件必须处理非以美国为中心的数据输入。

For instance, if the currency symbol is the Euro, or the Japanese Yen or the British Pound any of the other dozen currency symbols out there?

例如,如果货币符号是欧元、日元或英镑,那么其他十几种货币符号中的任何一个?

What about number formatting rules?

数字格式规则呢?

In the US you would enter 1,000,000.00but in France, this should be 1.000.000,00. Other countries allow spacing between digit-grouping...

在美国你会进入,1,000,000.00但在法国,这应该是1.000.000,00。其他国家/地区允许数字分组之间的间距...

If you use a straight Regex without taking the Culture into account, then you're never going to validate successfully unless you're 100% sure your software will never ever be used in a non-US centric context.

如果您在不考虑文化的情况下直接使用正则表达式,那么除非您 100% 确定您的软件永远不会在非以美国为中心的环境中使用,否则您永远不会成功验证。

回答by Brian Carlton

Be careful with floats. Eventually you will hit a case such as 0.01 represented as 0.00999999. Strings or integers are better to use.

小心浮动。最终,您将遇到一个案例,例如 0.01,表示为 0.00999999。最好使用字符串或整数。

回答by Brian Carlton

Use this regular expression for US currency \$(\d)*\d Matches $300,$12900 Non-Match $12900.00

对美元使用此正则表达式 \$(\d)*\d 匹配 $300,$12900 不匹配 $12900.00

回答by Ben Power

I think I've found a problem with ssg's solution (or perhaps an MS bug!).

我想我发现 ssg 的解决方案有问题(或者可能是 MS 错误!)。

Running this:

运行这个:

float.TryParse("0,2",NumberStyles.Currency, CultureInfo.GetCultureInfo("en-US"), out num)

Returns true. Surely "0,2" isn't a valid currency value?

返回真。“0,2”肯定不是有效的货币价值吗?

回答by Ghostrider

This regular Expression works for me:

这个正则表达式对我有用:

'^[$]{0,1}([0-9]+[,]?[0-9]+|[0-9]{1,3}([.][0-9]{3})*([,][0-9]+)?)$'

with switch

带开关

'^${0,1}(\d+,?[0-9]+|\d{1,3}(\.\d{3})*(,\d+)?)$'

it works for

它适用于

  • $1,000,000.150
  • 10000000.199
  • $10000
  • 1,000,000.150
  • 100000.123
  • 10000
  • 1,000,000.150 美元
  • 10000000.199
  • 10000 美元
  • 1,000,000.150
  • 100000.123
  • 10000