Numbers-Only Regex
Digits-only validation settled — the five-character gate plus a canonical-integer strict variant, CI-verified rows and live testing.
Pattern variants
Pragmatic
/^\d+$/
What it matches
One or more ASCII digits 0-9 and nothing else.
What it deliberately doesn't
Signs, decimals, exponents and thousands separators — this gates digit strings, not numbers.
Strict
/^(?:0|[1-9]\d*)$/
What it matches
A canonical unsigned integer: 0 alone, or a non-zero digit followed by more digits.
What it deliberately doesn't
Leading zeros (007) — those usually signal an identifier or padding, not an integer.
Verified vectors
| Input | Pragmatic | Strict | Note |
|---|---|---|---|
"42" | ✓ matches | ✓ matches | |
"0" | ✓ matches | ✓ matches | |
"007" | ✓ matches | ✗ no match | leading zeros — strict wants a canonical integer |
"-7" | ✗ no match | ✗ no match | signs are deliberately excluded |
"+7" | ✗ no match | ✗ no match | signs are deliberately excluded |
"3.14" | ✗ no match | ✗ no match | decimals are deliberately excluded |
"1,000" | ✗ no match | ✗ no match | thousands separators are deliberately excluded |
"12 34" | ✗ no match | ✗ no match | inner space |
"42 " | ✗ no match | ✗ no match | trailing space |
"" | ✗ no match | ✗ no match | empty string |
"١٢٣" | ✗ no match | ✗ no match | non-ASCII digits — JavaScript \d is [0-9] only |
"42
" | ✗ no match | ✗ no match | trailing newline — JavaScript $ means exact end |
Usage by language
JavaScript
Machine-verified/^\d+$/
/^(?:0|[1-9]\d*)$/
Python
Machine-verifiedre.compile(r"^\d+$")
re.compile(r"^(?:0|[1-9]\d*)$")
Python's str-pattern \d also matches non-ASCII digits such as ١٢٣, and $ also matches before a trailing newline — pass re.ASCII and anchor with \Z for JavaScript-identical behavior.
Java
Syntax-reviewed — not machine-verifiedPattern.compile("^\\d+$")
Pattern.compile("^(?:0|[1-9]\\d*)$")
Go
Syntax-reviewed — not machine-verifiedregexp.MustCompile(`^\d+$`)
regexp.MustCompile(`^(?:0|[1-9]\d*)$`)
C#
Syntax-reviewed — not machine-verifiednew Regex(@"^\d+$")
new Regex(@"^(?:0|[1-9]\d*)$")
PHP
Syntax-reviewed — not machine-verified'/^\\d+$/'
'/^(?:0|[1-9]\\d*)$/'
Caveats
- Digits-only means unsigned integers: signs, decimals and separators are deliberately excluded — use a number parser for amounts.
- JavaScript \d is exactly [0-9]; other engines can be Unicode-wide (see the Python note).
Related patterns
Do it offline:$ grep -E '^[0-9]+$' values.txt
How to use it
- Gate quantity fields, page numbers and numeric ids with the pragmatic line before Number() or parseInt ever touches the value.
- Switch to strict when 007 should fail — counters and indexes have one canonical spelling; padding usually marks a label.
- Keep zero-padded codes on the pragmatic side, where the padding is part of the identifier rather than an error.
Five characters answering one question: is this string digits and nothing else? The pragmatic ^\d+$ accepts 42 and 007 alike while refusing signs, decimals, separators and stray whitespace. The strict variant narrows to canonical unsigned integers, where a leading zero smells like an identifier, not a number. Six language spellings, twelve CI-verified rows, both variants covered.
Examples
^\d+$pattern /^\d+$/
42 ✓ matches
0 ✓ matches
3.14 ✗ no match
1,000 ✗ no matchEach refusal carries the lesson: a decimal point and a thousands comma are number formatting, and this gate reads raw digit strings only.
^(?:0|[1-9]\d*)$pragmatic /^\d+$/
strict /^(?:0|[1-9]\d*)$/
007 pragmatic ✓ matches strict ✗ no match
42 pragmatic ✓ matches strict ✓ matches
-7 pragmatic ✗ no match strict ✗ no matchThe leading-zero row is the entire gap between the variants: pragmatic sees three digits, strict sees a non-canonical integer. Signed values fail both, deliberately.
Every example on this page is executed against the same engine that powers the tool, on every build.
Gotchas worth knowing
JavaScript \d stops at ASCII
In this engine \d is exactly [0-9], so Arabic-Indic digits like ١٢٣ never match — the vectors table proves it. Python's str patterns reach wider by default; the compatibility note on this page shows the re.ASCII flag that restores agreement.
Common errors
Invalid regular expression: /^[0-9+$/: Unterminated character class- Reaching for + to mean one-or-more before closing the bracket leaves the class open, and everything after [ reads as a class member until a ] appears. Close it first — [0-9]+ — so the quantifier applies to the whole class.
FAQ
Why does the pattern refuse -7 and +7?
Signs are arithmetic notation, and this page gates digit strings — ids, counters, PINs — where a sign is corruption. Prepend an optional [-+]? yourself when signed input is truly the contract.
How do I allow decimals like 3.14?
Add an optional fraction group — ^\d+(?:\.\d+)?$ — in the tester and watch the marks flip. Past that, locale rules about grouping take over and a number parser earns its keep.
Is ^\d+$ safe on enormous inputs?
Yes — one character class under a single quantifier has nowhere to backtrack, so matching stays linear at any length. Regex slowness stories need nested quantifiers, and this page has none.