115

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.

Zeemee
  • 10,486
  • 14
  • 51
  • 81

14 Answers14

221

Try this:

^[1-9]\d*$

...and some padding to exceed 30 character SO answer limit :-).

Here is Demo

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
81

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]*$
Ray Toal
  • 86,166
  • 18
  • 182
  • 232
  • This seems to fulfill what the OP wanted – Honinbo Shusaku Aug 26 '15 at 13:47
  • i want positive integer and positive decimal. you have any solution? –  Nov 04 '15 at 08:28
  • You should ask that as a new question rather than as a comment. But spoiler: `^[0-9]*[1-9][0-9]*(\.[0-9]+)$` but that is making an assumption about what you mean by "decimal." Do you need exponent parts? This is pretty involved, so ask another question. – Ray Toal Nov 05 '15 at 04:08
  • 4
    +1 for considering corner case! BTW this pattern will work exactly the same: **`^0*[1-9]\d*$`** since the first `[0-9]*` is active only until `[1-9]` finds first non-zero i.e. it's active only until there are initial zeros (`0*` ). – mg007 Aug 09 '16 at 04:43
  • Yes, that's the best one. – Ray Toal Jul 26 '17 at 20:05
  • 1
    This is the right answer. It accepts 00098 which is the correct behavior as per question. – Varun Sharma Dec 30 '19 at 19:51
21

You might try a negative lookahead assertion:

^(?!0+$)\d+$
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
18

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
11

^\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..

manoj
  • 3,391
  • 2
  • 20
  • 30
  • For some reason this was not working in the pattern attribute of JSP page, the other pattern that helped , [0]*[1-9][0-9]* Ref URL: https://www.toolbox.com/tech/programming/question/regular-expression-nonzero-values-083109/ – rinilnath Jan 27 '22 at 03:58
4

Any positive integer, excluding 0: ^\+?[1-9]\d*$
Any positive integer, including 0: ^(0|\+?[1-9]\d*)$

Evgeny Minkevich
  • 2,319
  • 3
  • 28
  • 42
2

Got this one:

^[1-9]|[0-9]{2,}$

Someone beats it? :)

Zeemee
  • 10,486
  • 14
  • 51
  • 81
  • 3
    This would allow `00` Do you want this? And it will allow `1aaaaa` and `abcd01`. `^` belongs only to the first alternative and `$` only to the second, to solve this put brackets around it `^([1-9]|[0-9]{2,})$` – stema Aug 12 '11 at 06:32
  • Well, this accepts `000000000`. You did say any integer _excluding zero_. – Ray Toal Aug 12 '11 at 06:33
2

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) {...}
Lie Ryan
  • 62,238
  • 13
  • 100
  • 144
  • 1
    Two problems: This also matches "123abc", and returns 123, and this might throw a ParseException. – Daniel Aug 12 '11 at 06:31
  • @Daniel: I guess this might be used inside a larger parsing scheme, therefore you would have a regex/BNF that captures digits only, and a java code to check that the captured digits are non-zero positive. – Lie Ryan Aug 12 '11 at 06:37
  • @Daniel: in any case, you would still need the data as an integer and so sooner or later you would still need to call parseInt() or roll your own parseInt(). – Lie Ryan Aug 12 '11 at 10:00
  • @Daniel: `Integer.parseInt()` itself adds very little overhead. It's the throwing and catching of exceptions that's expensive. – Alan Moore Aug 12 '11 at 10:38
  • @Lie: what's the deal with the `\\+?` prefixes? I'm guessing that's supposed to be an escaped plus sign as it would appear in Java source code, but why? If minus signs aren't allowed, I think it's safe to assume plus signs are out, too. – Alan Moore Aug 12 '11 at 10:50
  • @Alan Moore: +10 == 10 so if the OP want to have a lax matching, then he might want to allow an optional plus sign. – Lie Ryan Aug 12 '11 at 12:54
  • I think lax *specifying* is our real problem. ;) But @Daniel is right about the error in your regex. If you don't enclose the alternation in its own group, like so: `^\+?(?:[1-9]|\d+)$`, it will match any string that *starts* with `[1-9]` (like `1foo`) or *ends* with `[0-9]` (like `bar2`). @Mulmoth's [self-answer](http://stackoverflow.com/questions/7036324/what-is-the-regex-for-any-positive-integer-excluding-0/7036362#7036362) has the same problem. – Alan Moore Aug 13 '11 at 00:16
  • @Alan Moore,@Daniel: ah, I see what you meant, fixed. – Lie Ryan Aug 13 '11 at 03:39
2

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

porges
  • 30,133
  • 4
  • 83
  • 114
1

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

Emma
  • 27,428
  • 11
  • 44
  • 69
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]))$
OleksiiMLD
  • 80
  • 1
  • 7
0

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:

  • 2000000000 to 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}
  • 1000000000 to 1999999999 -> 1\d{9}
  • 1 to 999999999 -> [1-9]\d{0,8}
Dima
  • 311
  • 2
  • 5
-2

^[1-9]*$ is the simplest I can think of

-3

This should only allow decimals > 0

^([0-9]\.\d+)|([1-9]\d*\.?\d*)$
FelixSFD
  • 6,052
  • 10
  • 43
  • 117