Click here to Skip to main content
14,868,645 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want the regular exp which accept only numbers 0-9 without any special character or decimal.Also single 0 should not be allowed but 0 followed with other numbers are allow.
Did you try anything?


[edit]Added second part to regex - OriginalGriff[/edit]
Hi @OriginalGriff,

Your regex is correct, which satisfies all conditions except one.
That is - it excludes the 0, if there are any numbers 1-9 is present after it.
You can check online here -
All the accepted numbers are highlighted at the right box.
You can check that "01567" is matched like "1567", excluding the 0.

I have considered this condition and submitting my answer here. Please have a look and comment.

OriginalGriff 31-Jan-13 4:06am
Sorry - it's early morning here! :laugh:
Answer updated.
BTW, if you are going to play with regexes, et a copy of Expresso
It's free, and it examines and generates Regular expressions.
Thanks for the info.
Your update answer still matches number of zeros. ex - 000000 should not be accepted, right as OP writes "0 followed with other numbers are allow."?
Regular expression - (^[0][1-9]+)|([1-9]\d*)
Test online - Here[^].

12345678 - 12345678
0 - Not mached.
01567 - 01567
0000 - Not matched.
bbbb - Not matched.
1 9999 - Space character is not matched, instead it is taken as two different numbers as "1", "9999".

-> All the test strings are written in left box.
-> All the matched results are written in right box.
-> You can also check, which group got executed for the matched string (in box "Match groups" below the right "Match result" box),
where Group-1 is (^[0][1-9]+) and Group-2 is ([1-9]\d*).

For example - 12345678 is matched by Group-2 and not by Group-1.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900