118

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.

3
  • 2
    Do you want to accept, for example, 076? Commented Aug 12, 2011 at 6:25
  • @Karl Knechtel: But I can sacrifice this feature for the sake of simplicity.
    – Zeemee
    Commented Aug 12, 2011 at 6:34
  • 3
    “Any positive integer, excluding 0” note that 0 is not a positive integer.
    – Ken
    Commented Apr 28, 2014 at 5:03

14 Answers 14

224

Try this:

^[1-9]\d*$

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

Here is Demo

8
  • 1
    Just out of curiosity, why do we need \d+ at the end? Why wouldn't ^[1-9]+$ work?
    – mmtauqir
    Commented Aug 12, 2011 at 6:26
  • 26
    @mtahmed: ^[1-9]+$ would not allow 10
    – Lie Ryan
    Commented Aug 12, 2011 at 6:28
  • 1
    It will allow 1. "\d*" does also match the empty string.
    – Daniel
    Commented Aug 12, 2011 at 6:29
  • 4
    But what about 01?
    – Museful
    Commented May 28, 2017 at 7:58
  • 1
    To allow 0, change the expression into ^[0-9]\d*$ or ^\d*[1-9]\d*$ then it will accept 01 and 10. Commented May 31, 2021 at 10:58
82

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]*$
6
  • This seems to fulfill what the OP wanted Commented Aug 26, 2015 at 13:47
  • i want positive integer and positive decimal. you have any solution?
    – user4853635
    Commented Nov 4, 2015 at 8: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
    Commented Nov 5, 2015 at 4: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
    Commented Aug 9, 2016 at 4:43
  • 1
    This is the right answer. It accepts 00098 which is the correct behavior as per question. Commented Dec 30, 2019 at 19:51
22

You might try a negative lookahead assertion:

^(?!0+$)\d+$
2
  • 1
    This allows 01, 02, etc…
    – Matijs
    Commented Sep 5, 2016 at 11:31
  • @Matijs the question was written such that this is okay and expected. To disallow any leading zeros, it should suffice to simplify the lookahead to (?!0). Commented Sep 26, 2023 at 4:25
19

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
5
  • 3
    Thank you, but it doesn't add any value or elegance when compared to the accepted answer [1-9]\d*.
    – Zeemee
    Commented Mar 14, 2016 at 8:09
  • 2
    @Zeemee that is not correct as \d might include numerals that are not 0-9. For example Arabic numerals like واحد in some libraries.
    – dummyDev
    Commented Feb 6, 2020 at 12:19
  • @dummyDev I wasn't aware of that. Thank you for mentioning!
    – Zeemee
    Commented Feb 7, 2020 at 13:19
  • Sadly this will return true for "123abcd". Commented Mar 11, 2020 at 11:06
  • Charith Jayasanka - yes, it still needs a ^ at the beginning and a "$" at the end :-) Commented Jul 10, 2020 at 13:44
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..

1
4

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

3

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) {...}
8
  • 1
    Two problems: This also matches "123abc", and returns 123, and this might throw a ParseException.
    – Daniel
    Commented Aug 12, 2011 at 6: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
    Commented Aug 12, 2011 at 6: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
    Commented Aug 12, 2011 at 10:00
  • @Daniel: Integer.parseInt() itself adds very little overhead. It's the throwing and catching of exceptions that's expensive.
    – Alan Moore
    Commented Aug 12, 2011 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
    Commented Aug 12, 2011 at 10:50
2

Got this one:

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

Someone beats it? :)

2
  • 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
    Commented Aug 12, 2011 at 6:32
  • Well, this accepts 000000000. You did say any integer excluding zero.
    – Ray Toal
    Commented Aug 12, 2011 at 6:33
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].

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

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]))$
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}
-2

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

1
  • This regular expression will erroneously fail to match numbers such as 10 and 29303. It will also match empty string. Commented Nov 26, 2013 at 23:05
-3

This should only allow decimals > 0

^([0-9]\.\d+)|([1-9]\d*\.?\d*)$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.