Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

regex

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.

like image 947
Zeemee Avatar asked Aug 12 '11 06:08

Zeemee


People also ask

What does \+ mean in regex?

Example: The regex "aa\n" tries to match two consecutive "a"s at the end of a line, inclusive the newline character itself. Example: "a\+" matches "a+" and not a series of one or "a"s. ^ the caret is the anchor for the start of the string, or the negation symbol.

What does %s mean in regex?

The Difference Between \s and \s+ For example, expression X+ matches one or more X characters. Therefore, the regular expression \s matches a single whitespace character, while \s+ will match one or more whitespace characters.

What does the regex 0 9 ]+ do?

In this case, [0-9]+ matches one or more digits. A regex may match a portion of the input (i.e., substring) or the entire input. In fact, it could match zero or more substrings of the input (with global modifier). This regex matches any numeric substring (of digits 0 to 9) of the input.


1 Answers

Try this:

^[1-9]\d*$ 

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

Here is Demo

like image 148
Tomasz Nurkiewicz Avatar answered Oct 21 '22 14:10

Tomasz Nurkiewicz