.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])?$
精彩评论