Reputation: 19
I want a regular expression which accepts only numbers 0-9 without any special character or decimal. Also single 0 should not be allowed but 0 followed by other numbers are allowed.
Upvotes: 1
Views: 5767
Reputation: 3333
0*[1-9]\d*
should do the work.
[1-9]
would enforce that a number must start with 1-9; or if it starts with 0, must be followed with a number between 1-9. \d*
then allows the occurrence of any number including 0 in the complete number.
Upvotes: 7
Reputation: 3994
I usually work in C++ using the boost/regex.hpp library. This should work well for it.
boost::regex e("(\d*)([1-9])(\d*)");
Upvotes: 0