What is the regex for "Any positive integer, excluding 0"
Asked Answered
A

14

117

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.

Adon answered 12/8, 2011 at 6:22 Comment(3)
Do you want to accept, for example, 076?Sectary
@Karl Knechtel: But I can sacrifice this feature for the sake of simplicity.Adon
“Any positive integer, excluding 0” note that 0 is not a positive integer.Defazio
B
224

Try this:

^[1-9]\d*$

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

Here is Demo

Benn answered 12/8, 2011 at 6:24 Comment(8)
Just out of curiosity, why do we need \d+ at the end? Why wouldn't ^[1-9]+$ work?Twirp
@mtahmed: ^[1-9]+$ would not allow 10Congregational
It will allow 1. "\d*" does also match the empty string.Optical
@mtahmed ^[1-9]+$ would not allow 10. @Mulmoth the suggestion will allow 1, since \d* matches zero or more times. However, it will not allow 076, as that doesn't start with a [1-9].Sectary
@Karl Knechtel: He changed ^[1-9]\d+$ to ^[1-9]\d*$ after my comment, which I deleted afterwards.Adon
But what about 01?Baer
Why ^[1-9]\d?$ does not allow 100 and more? It's a question mark ? sign after \dRodmur
To allow 0, change the expression into ^[0-9]\d*$ or ^\d*[1-9]\d*$ then it will accept 01 and 10.Undetermined
D
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]*$
Dynamotor answered 12/8, 2011 at 6:31 Comment(6)
This seems to fulfill what the OP wantedBaluster
i want positive integer and positive decimal. you have any solution?Mascle
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.Dynamotor
+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* ).Oni
Yes, that's the best one.Dynamotor
This is the right answer. It accepts 00098 which is the correct behavior as per question.Theocrasy
S
22

You might try a negative lookahead assertion:

^(?!0+$)\d+$
Sectary answered 12/8, 2011 at 6:32 Comment(2)
This allows 01, 02, etc…Celerity
@Celerity 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).Sectary
P
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
Piddock answered 14/3, 2016 at 6:3 Comment(5)
Thank you, but it doesn't add any value or elegance when compared to the accepted answer [1-9]\d*.Adon
@Adon that is not correct as \d might include numerals that are not 0-9. For example Arabic numerals like واحد in some libraries.Announcer
@Announcer I wasn't aware of that. Thank you for mentioning!Adon
Sadly this will return true for "123abcd".Gnash
Charith Jayasanka - yes, it still needs a ^ at the beginning and a "$" at the end :-)Tuscan
C
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..

Complected answered 26/8, 2015 at 13:45 Comment(1)
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: toolbox.com/tech/programming/question/…Additional
I
4

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

Intosh answered 30/7, 2019 at 8:26 Comment(0)
C
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) {...}
Congregational answered 12/8, 2011 at 6:28 Comment(8)
Two problems: This also matches "123abc", and returns 123, and this might throw a ParseException.Optical
@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.Congregational
@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().Congregational
@Daniel: Integer.parseInt() itself adds very little overhead. It's the throwing and catching of exceptions that's expensive.Lafollette
@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.Lafollette
@Alan Moore: +10 == 10 so if the OP want to have a lax matching, then he might want to allow an optional plus sign.Congregational
I think lax specifying is our real problem. ;) But @Optical 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 has the same problem.Lafollette
@Alan Moore,@Daniel: ah, I see what you meant, fixed.Congregational
A
2

Got this one:

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

Someone beats it? :)

Adon answered 12/8, 2011 at 6:25 Comment(2)
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,})$Eggers
Well, this accepts 000000000. You did say any integer excluding zero.Dynamotor
V
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].

Vivavivace answered 12/8, 2011 at 6:32 Comment(0)
R
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).

Ripley answered 27/4, 2019 at 20:45 Comment(0)
R
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]))$
Robbirobbia answered 29/4, 2019 at 15:8 Comment(0)
S
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}
Solo answered 25/7, 2023 at 10:38 Comment(0)
P
-2

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

Porkpie answered 26/11, 2013 at 22:29 Comment(1)
This regular expression will erroneously fail to match numbers such as 10 and 29303. It will also match empty string.Varicolored
V
-3

This should only allow decimals > 0

^([0-9]\.\d+)|([1-9]\d*\.?\d*)$
Versicle answered 8/1, 2017 at 18:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.