What Is the Regex for "Any Positive Integer, Excluding 0"

What is the regex for Any positive integer, excluding 0

Try this:

^[1-9]\d*$

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

Here is Demo

What is the regex for “Any positive integer, excluding 0”

Assuming the language is JavaScript, you need to escape the backslash character within a string for it to have a value of backslash:

'\d' is a string with a value of d
'\\d' is a string with a value of \d

var pattern = new RegExp('^[1-9]\\d*$');

JavaScript also has regular expression literals, which avoid the need for additional escape characters:

var pattern = /^[1-9]\d*$/;

Regular expression in Java for positive integers (excluding those starting with zero)

Right now you use the expression [0-9]{1,9}, so this clearly allows the number 0134. When you want to require that the first character is not a zero, you have to use [1-9][0-9]{0,8}.

By the way: 0134 is a positive integer. It is an integer number, and it is positive. ;)

edit:

To prevent integer overflow you can use this pattern:

  • [1-9][0-9]{0,8}
  • [1-1][0-9]{9}
  • [2-2][0-1][0-9]{8}
  • [2-2][1-1][0-3][0-9]{7}
  • [2-2][1-1][4-4][0-6][0-9]{6}

I think you get the idea. Then you combine these expressions with |, and you are done.

Alternatively, have a look at the Integer.valueOf(String) method, how it parses numbers and checks for overflow. Copy that code and change the part with the NumberFormatException.

Regular expression for non-zero positive floats

Use

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

See proof.

Explanation

--------------------------------------------------------------------------------
^ the beginning of the string
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
[1-9] any character of: '1' to '9'
--------------------------------------------------------------------------------
\d* digits (0-9) (0 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
| OR
--------------------------------------------------------------------------------
0 '0'
--------------------------------------------------------------------------------
(?! look ahead to see if there is not:
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
0+ '0' (1 or more times (matching the
most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of
the string
--------------------------------------------------------------------------------
) end of look-ahead
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
--------------------------------------------------------------------------------
\. '.'
--------------------------------------------------------------------------------
\d+ digits (0-9) (1 or more times (matching
the most amount possible))
--------------------------------------------------------------------------------
)? end of grouping
--------------------------------------------------------------------------------
$ before an optional \n, and the end of the
string

Exclude only 0 in regular expression javascript

You may use a negative lookahead:

^(?!0+$)[0-9]{1,10}$

See the regex demo

Details:

  • ^ - start of string
  • (?!0+$) - no just zeros are allowed up to the end of string
  • [0-9]{1,10} - 1 to 10 digits
  • $ - end of string.

NOTE: To also allow empty value, use 0 as the min argument in the limiting quantifier:

^(?!0+$)[0-9]{0,10}$
^

See How Negative Lookahead Works (more here) to learn more about how (?!0+) works in this pattern. In short: right at the start of the string, we check the whole string for just zeros. If there is a zero or more right after start of a string, the match is failed. Else, 1 (or 0) to 10 digits are matched and the result is returned.

What's the regular expression for positive whole numbers only? (zero is not allowed)

May be you are looking for ^[1-9]+$ pattern. It'll match 1245, 2253 etc. But not 1003. or 0124. If you want to match the 3rd number (1003) as well use ^[1-9]\d*$ pattern.

In php you'd use

preg_match('/^[1-9]\d*$/', $subject, $matches)

regex which accept positive integer and nothing

Simply do:

^\d*$

The * means: "zero or more times".

Since you've asked most questions with the Java tag, I'm assuming you're looking for a Java solution. Note that inside a string literal, the \ needs to be escaped!

A demo:

class Test {
public static void main(String[] args) {
String[] tests = {"-100", "", "2334", "0"};
for(String t : tests) {
System.out.println(t + " -> " + t.matches("\\d*"));
}
}
}

produces:

-100 -> false
-> true
2334 -> true
0 -> true

Note that matches(...) already validates the entire input string, so there's no need to "anchor" it with ^ and $.

Beware that it would also return true for numbers that exceed Integer.MAX_VALUE and Long.MAX_VALUE. SO even if matches(...) returned true, parseInt(...) or parseLong(...) may throw an exception!



Related Topics



Leave a reply



Submit