Regex to find numbers in text

Use /-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?/g to find every match in a text. Every example below is tested against the pattern, and you can open it in the Regex Tester with the samples preloaded.

Updated

/-?(?:\d{1,3}(?:,\d{3})+|\d+)(?:\.\d+)?/g
Open in Regex Tester

Sample text

Order 3 items at $4.50 each, save -2.5% on 1,200 units — 12345 in stock.

5 matches

3 4.50 -2.5 1,200 12345

How the pattern works

PartMeaning
-?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.