开发者

.NET Regex, only numeric, no spaces

开发者 https://www.devze.com 2022-12-09 16:09 出处:网络
I\'m looking for a regular expression that accepts only numerical values and no spaces. I\'m currently using:

I'm looking for a regular expression that accepts only numerical values and no spaces. I'm currently using:

^(0|[1-9][0-9]*)$

which works fine, but it accepts values that consist 开发者_如何学JAVAONLY of spaces. What is wrong with it?


The reason why is that * will accept 0 or more. A purely empty string has 0 numbers and hence meets the requirements. You need 1 or more so use + instead.

^(0|[1-9][0-9]+)$

EDIT

Here is Andrews more robust and simpler solution.

^\d+$


this regex works perfecttly

^\d*[0-9](|.\d*[0-9])?$
0

精彩评论

暂无评论...
验证码 换一张
取 消