Reputation: 10714
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.
Upvotes: 118
Views: 219434
Reputation: 319
Ugly, but match the exact range 1..2147483647
^(214748364[0-7]|21474836[0-3]\d|2147483[0-5]\d{2}|214748[0-2]\d{3}|21474[0-7]\d{4}|2147[0-3]\d{5}|214[0-6]\d{6}|21[0-3]\d{7}|20\d{8}|1\d{9}|[1-9]\d{0,8})$
Note:
214748364[0-7]|21474836[0-3]\d|2147483[0-5]\d{2}|214748[0-2]\d{3}|21474[0-7]\d{4}|2147[0-3]\d{5}|214[0-6]\d{6}|21[0-3]\d{7}|20\d{8}
1\d{9}
[1-9]\d{0,8}
Upvotes: 0
Reputation: 340723
Try this:
^[1-9]\d*$
...and some padding to exceed 30 character SO answer limit :-).
Upvotes: 227
Reputation: 41
Any positive integer, excluding 0: ^\+?[1-9]\d*$
Any positive integer, including 0: ^(0|\+?[1-9]\d*)$
Upvotes: 4
Reputation: 80
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]))$
Upvotes: 0
Reputation: 11
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).
Upvotes: 1
Reputation: 7
This should only allow decimals > 0
^([0-9]\.\d+)|([1-9]\d*\.?\d*)$
Upvotes: -3
Reputation: 372
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
Upvotes: 19
Reputation: 3761
^\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..
Upvotes: 11
Reputation: 64837
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) {...}
Upvotes: 3
Reputation: 61508
You might try a negative lookahead assertion:
^(?!0+$)\d+$
Upvotes: 22
Reputation: 30580
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]
.
Upvotes: 2
Reputation: 88378
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]*$
Upvotes: 81