regexhelper
Test a pattern. Copy a ready-made one. No signup.

Regex to Match Digits Only

Copy the pattern, paste your values, and see which are purely numeric. Below: how it differs from matching a number.

/ /

What this pattern matches

It matches a whole string of digits only — useful for numeric IDs, PINs, and zip-style codes where leading zeros matter.

Part by part

^\d+$ — anchored, one or more digit characters (0–9).

Digits vs numbers

This is not the same as a number: 12.5 and -3 fail because the dot and sign are not digits. For a decimal number use -?\d+(?:\.\d+)?.

Use it in your code

Python
import re
text = "12345\n007\n12.5\n42abc\n0"
pattern = r'''^\d+$'''
for m in re.finditer(pattern, text, re.M):
    print(m.group())
JavaScript
const text = "12345\n007\n12.5\n42abc\n0";
const re = new RegExp(String.raw`^\d+$`, "gm");
console.log(text.match(re));

FAQ

Does \d match more than 0-9?

In Unicode mode \d can match other decimal digits (e.g. Arabic-Indic). Use [0-9] to stay strictly ASCII.

How do I require an exact length?

Use a quantifier: ^\d{6}$ matches exactly six digits.

Why does 12.5 fail?

The decimal point is not a digit, and the anchors require the whole string to be digits.