用于检查字符串是否仅包含零的正则表达式

Regular expression to check if string contains only zeros
2020-10-18
  •  译文(汉语)
  •  原文(英语)

我需要验证金额字段,该字段不应为零金额.例如.它CAN NOT0000,0.00,000.000,0CAN0.0001,1.000,1.00,1234.00等价值观.

尝试过@"[^1-9]+",@"0+((\.0+)"但它们会使包含零的每个值无效.

速聊1:
为什么不将字符串转换为数字并检查它是否为0?
速聊2:
我认为您以后无论如何都需要使用金额,因此检查数字是否为0将是一个更好的解决方案.
速聊3:
尝试解析该字符串.如果解析成功,则将其乘以某个数字;如果得到零,则它只有零.最简单的解决方案;)
速聊4:
-我很好奇,乘法在哪里出现?当然要得到0,您本来应该有0?
速聊5:
在"因为我们可以做到"的意义上,您可以通过检查是否有1到9之间的任何字符来进行分析而无需解析或正则表达式 bool hasnumbers = inputstring.Any(c => c >= '1' && c <= '9');
解决过程1

我不明白您为什么需要正则表达式,只需将字符串转换为数字并检查是否为0:

decimal actNumber;
if(decimal.TryParse(myAmount, out actNumber) && actNumber > 0) 
{ /* ... */ }

因此,您以后也可以使用实际数字.

解决过程2
^(?=.*?[1-9])\d+(\.\d+)?$

您可以lookahead为此使用一个简单的方法,它将验证是否至少有一个[1-9].

解决过程3

如果希望正则表达式检查仅包含一个字符的字符串,则只需指定该字符位于开头,结尾以及中间的任何位置.这是关于数字0的操作示例:

正则表达式'^ 0 + $'

如果您担心包含非零数字的值,可以使用以下方法确保不存在此类字符:

正则表达式'^ [^ 1-9] + $'

速聊1:
您在回答什么问题?
速聊2:
我不是OP,但我想说谢谢,我什至没有想到这一点.对我有很大帮助.

I need to validate an Amount field, which should not be Zero amount. For eg. it CANNOT be 0000,0.00,000.000,0 BUT it CAN be 0.0001, 1.000,1.00,1234.00 etc values.

Tried @"[^1-9]+" and @"0+((\.0+)" But they invalidate every value which contains a zero.

Talk1:
Why don't you just convert the string to a number and check if it is 0?
Talk2:
I presume you are going to need to use the amount afterwards anyway so checking if the number is 0 would be a much better solution.
Talk3:
Try parsing the string. If parse is successful, multiply it with some number, if you get zero it has zeroes only. Simplest solution ;)
Talk4:
- I'm curious, where does the multiplication come into it? Surely to get 0, you would have had a 0 to begin with?
Talk5:
In the sense of 'because we can', you could do it without parsing or regex, by checking if any char is between 1 and 9 bool hasnumbers = inputstring.Any(c => c >= '1' && c <= '9');
Solutions1

I don´t see why you need a regex, simply convert the string to a number and check if that is 0:

decimal actNumber;
if(decimal.TryParse(myAmount, out actNumber) && actNumber > 0) 
{ /* ... */ }

Thus you can also use the actual number afterwards.

Solutions2
^(?=.*?[1-9])\d+(\.\d+)?$

You can use a simple lookahead for this which will validate if there is at least one [1-9].

Solutions3

If you want a regular expression to check for strings containing only one character, you can just specify that the character be located at the beginning, end, and everywhere in between. Here is an example of how to do so for the digit 0:

regexp '^0+$'

If you are worried about the value containing non-zero digits, you can ensure that no such characters are present using:

regexp '^[^1-9]+$'

Talk1:
What question are you answering?
Talk2:
I'm not OP but I'd like to say a late thanks, I didn't even think of this. Helped me a lot.
转载于:https://stackoverflow.com/questions/30773750/regular-expression-to-check-if-string-contains-only-zeros

本人是.net程序员,因为英语不行,使用工具翻译,希望对有需要的人有所帮助
如果本文质量不好,还请谅解,毕竟这些操作还是比较费时的,英语较好的可以看原文

留言回复
我们只提供高质量资源,素材,源码,坚持 下了就能用 原则,让客户花了钱觉得值
上班时间 : 周一至周五9:00-17:30 期待您的加入