[英]C# RegEx for decimal number or zero (0)
This gets all numbers but not floating numbers 这将获取所有数字,但不能获取浮点数字
Regex(@"^\d+$")
I need it to get these values as well: 我也需要它来获得这些值:
1234.12345545
0
12313
12313.13
-12131
-1313.13211312
For matching all of the above; 用于匹配以上所有内容; the most appropriate regex is probably
最合适的正则表达式可能是
@"^[+-]?\d+(\.\d+)?$"
This matches all of the above; 这符合以上所有条件; but not numbers on the format
.3456
. 但不能使用
.3456
格式的.3456
。
It also matches numbers on the format +123 and -1234.5678 它还匹配+123和-1234.5678格式的数字
Try this here 在这里试试
^(?:[-+]?[1-9]\d*|0)?(?:\.\d+)?$
This will additionally match the empty string. 这将另外匹配空字符串。
See it online here on Regexr 在Regexr上在线查看
If matching the empty string is not wanted, then you can add a length check to your regex like 如果不希望匹配空字符串,则可以将长度检查添加到正则表达式中,例如
^(?=.+)(?:[-+]?[1-9]\d*|0)?(?:\.\d+)?$
The positive lookahead (?=.+)
ensures that there is at least 1 character 正向超前
(?=.+)
确保至少有1个字符
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.