DD
DevDash

Regex for Positive Integer

Regex Pattern

^[1-9]\d*$

Integers greater than zero (no leading zeros)

Quick Answer

The regex pattern for positive integer is `^[1-9]\d*$`. Integers greater than zero (no leading zeros). This works in JavaScript, Python, Ruby, PHP, Java, and most regex engines that support PCRE syntax.

Test Examples

InputResult
1✓ Matches
42✓ Matches
12345✓ Matches
999999✓ Matches
0✗ No match
-1✗ No match
1.5✗ No match
01✗ No match

Code Examples

javascript

const regex = /^[1-9]\d*$/;
const isValid = regex.test(value);

python

import re
pattern = r'^[1-9]\d*$'
if re.match(pattern, value):
    print("valid")

ruby

pattern = /^[1-9]\d*$/
if value =~ pattern
  puts "valid"
end

php

if (preg_match('/^[1-9]\d*$/', $value)) {
    echo "valid";
}

java

String pattern = "^[1-9]\\d*$";
boolean isValid = value.matches(pattern);

Frequently Asked Questions

Related Regex Patterns

Want API access + no ads? Pro coming soon.