How the pattern works
| Part | Meaning |
|---|---|
-? | Optional minus sign |
\d{1,3}(?:,\d{3})+ | Numbers with thousands separators, like 1,200 |
|\d+ | Or plain digits |
(?:\.\d+)? | Optional decimal part |
Code examples
JavaScript
const matches = text.match(/-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?/g) ?? [];Python
import re
matches = re.findall(r'-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?', text)PHP
preg_match_all('/-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?/', $text, $m);
$matches = $m[0];
Notes and limitations
Remove the commas before converting a match to a number. For numbers written as 1.200,50 (German and many other locales), swap the comma and the dot in the pattern.
Regex TesterType a pattern and watch matches highlight live in your test text.
Open the tool Frequently asked questions
What is the regex to find numbers in text?
/-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?/g — it finds every match in a text. See the table above for what each part does.
Does it work in Python and PHP?
Yes — the code examples above use the same pattern with re in Python and preg functions in PHP.
How do I test this regex?
Click “Open in Regex Tester” — the pattern, flags and sample text load automatically, and matches highlight as you edit.