Regex Numbers Only — Match Digits Pattern with Examples

^\d+$ is the simplest pattern for digits-only strings. For decimals, add (\.\d+)?. For negatives, prefix with -?. The examples below cover every common numeric validation scenario.

5 Number Regex Patterns

Digits only (integers)

/^\d+$/

Matches

123

0

9876543210

Does not match

12.5

-5

12a

(empty string)

Matches one or more digit characters (0–9). No decimals, no signs.

Optional leading zeros allowed

/^[0-9]+$/

Matches

007

000

42

Does not match

12.0

12a

(empty string)

Same as \d+ but explicitly uses the [0-9] character class.

Integer (optional negative sign)

/^-?\d+$/

Matches

-5

0

100

-999

Does not match

1.5

--5

1a

Allows an optional minus sign for negative integers.

Decimal number (integer or float)

/^-?\d+(\.\d+)?$/

Matches

3.14

-2.5

100

0.001

Does not match

3.

.5

1.2.3

abc

Matches integers and decimals. Requires at least one digit after the decimal point.

Phone number digits (7–15 digits)

/^\d{7,15}$/

Matches

1234567

12345678901234

Does not match

123456

1234567890123456

123-456

Validates raw digit strings for phone numbers. Does not allow spaces or dashes.

Code Examples

JavaScript

// Integers only
const integerOnly = /^\d+$/;
console.log(integerOnly.test("123"));   // true
console.log(integerOnly.test("12.5"));  // false

// Decimal numbers
const decimal = /^-?\d+(\.\d+)?$/;
console.log(decimal.test("3.14"));   // true
console.log(decimal.test("-2.5"));   // true
console.log(decimal.test("1."));     // false

// Extract all numbers from a string
const text = "Order 3 items at $12.50 each, total $37.50";
const numbers = text.match(/-?\d+(\.\d+)?/g);
console.log(numbers); // ["3", "12.50", "37.50"]

Python

import re

# Integers only
def is_integer(s):
    return bool(re.fullmatch(r'\d+', s))

print(is_integer("123"))   # True
print(is_integer("12.5"))  # False

# Decimal numbers
def is_number(s):
    return bool(re.fullmatch(r'-?\d+(\.\d+)?', s))

print(is_number("3.14"))  # True
print(is_number("-2.5"))  # True

# Extract all numbers from a string
text = "3 apples at $1.50 each"
numbers = re.findall(r'-?\d+(?:\.\d+)?', text)
print(numbers)  # ['3', '1.50']

Test Your Pattern Online

Related Guides

Frequently Asked Questions

What is the simplest regex for numbers only?

^\d+$ matches one or more digits with no other characters. For decimals, use ^-?\d+(\.\d+)?$.

How do I match negative numbers with regex?

Add -? (optional minus sign) at the start: ^-?\d+$. The ? makes the minus optional.

What is the difference between \d and [0-9]?

In most contexts they are equivalent. \d matches Unicode digits in some engines; [0-9] is explicit about ASCII digits only. Prefer [0-9] when you want only 0–9.

All tools run in your browser. Your data never leaves your device.