15,559,931 members
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.
Posted
Sergey Alexandrovich Kryukov 31-Jan-13 1:41am
Did you try anything?
—SA

## Solution 1

Try:
`[1-9]\d*|0\d+`

Added second part to regex - OriginalGriff[/edit]

v2
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 - http://www.rubular.com/r/VgXHDpSFGh
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.

Thanks,
OriginalGriff 31-Jan-13 4:06am
Sorry - it's early morning here! :laugh:
BTW, if you are going to play with regexes, et a copy of Expresso
http://www.ultrapico.com/Expresso.htm
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."?

## Solution 2

Solution
Regular expression - `(^[0][1-9]+)|([1-9]\d*)`
Test online - Here[^].

Examples
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".

Note
-> 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.

Thanks...