开发者

.NET Regex, only numeric, no spaces

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜