user2027902
user2027902

Reputation: 19

Regex for accepting only numbers

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

Answers (2)

Ali Shah Ahmed
Ali Shah Ahmed

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

Roney Michael
Roney Michael

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

Related Questions