How can ^\d+$
be improved to disallow 0
?
EDIT (Make it more concrete):
Examples to allow:
1
30
111
Examples to disallow:
0
00
-22
It doesn't matter if positive numbers with a leading zero are allowed or not (e.g. 022
).
This is for Java JDK Regex implementation.
You might want this (edit: allow number of the form 0123
):
^\\+?[1-9]$|^\\+?\d+$
however, if it were me, I would instead do
int x = Integer.parseInt(s)
if (x > 0) {...}
My pattern is complicated, but it covers exactly "Any positive integer, excluding 0" (1 - 2147483647, not long). It's for decimal numbers and doesn't allow leading zeros.
^((1?[1-9][0-9]{0,8})|20[0-9]{8}|(21[0-3][0-9]{7})|(214[0-6][0-9]{6})
|(2147[0-3][0-9]{5})|(21474[0-7][0-9]{4})|(214748[0-2][0-9]{3})
|(2147483[0-5][0-9]{2})|(21474836[0-3][0-9])|(214748364[0-7]))$
This should only allow decimals > 0
^([0-9]\.\d+)|([1-9]\d*\.?\d*)$
This RegEx matches any Integer positive out of 0:
(?<!-)(?<!\d)[1-9][0-9]*
It works with two negative lookbehinds, which search for a minus before a number, which indicates it is a negative number. It also works for any negative number larger than -9 (e.g. -22).
Sorry to come in late but the OP wants to allow 076
but probably does NOT want to allow 0000000000
.
So in this case we want a string of one or more digits containing at least one non-zero. That is
^[0-9]*[1-9][0-9]*$
Try this one, this one works best to suffice the requiremnt.
[1-9][0-9]*
Here is the sample output
String 0 matches regex: false
String 1 matches regex: true
String 2 matches regex: true
String 3 matches regex: true
String 4 matches regex: true
String 5 matches regex: true
String 6 matches regex: true
String 7 matches regex: true
String 8 matches regex: true
String 9 matches regex: true
String 10 matches regex: true
String 11 matches regex: true
String 12 matches regex: true
String 13 matches regex: true
String 14 matches regex: true
String 15 matches regex: true
String 16 matches regex: true
String 999 matches regex: true
String 2654 matches regex: true
String 25633 matches regex: true
String 254444 matches regex: true
String 0.1 matches regex: false
String 0.2 matches regex: false
String 0.3 matches regex: false
String -1 matches regex: false
String -2 matches regex: false
String -5 matches regex: false
String -6 matches regex: false
String -6.8 matches regex: false
String -9 matches regex: false
String -54 matches regex: false
String -29 matches regex: false
String 1000 matches regex: true
String 100000 matches regex: true
^[1-9]*$ is the simplest I can think of
Any positive integer, excluding 0: ^\+?[1-9]\d*$
Any positive integer, including 0: ^(0|\+?[1-9]\d*)$
Just for fun, another alternative using lookaheads:
^(?=\d*[1-9])\d+$
As many digits as you want, but at least one must be [1-9]
.
Got this one:
^[1-9]|[0-9]{2,}$
Someone beats it? :)
You might try a negative lookahead assertion:
^(?!0+$)\d+$
^\d*[1-9]\d*$
this can include all positive values, even if it is padded by Zero in the front
Allows
1
01
10
11 etc
do not allow
0
00
000 etc..
Source: Stackoverflow.com