Start free · Free tools · Fair usage limits
Browse Tools

Categories

Custom AI Solutions – AI2Flows
LIVE NEWS
Loading latest AI & SEO news…
Home›Tools›Regex Tester›Regex: Extract numbers from text
Regex pattern · find & extract

Regex: Extract numbers from text

Regex to extract numbers from a string: a tested pattern with examples, a plain-English breakdown, and code for JavaScript, Python, PHP, Java and Go.

Quick answer

Use /-?\d+(?:\.\d+)?/g with the global flag to find every match. In "Price: 19.99, qty -3 and 42 items" it finds "19.99", "-3", "42". A hyphen is read as a minus sign, so "2026-11-25" gives 2026, -11 and -25.

Last updated · Maintained by FreeAIToolz · How we test

/-?\d+(?:\.\d+)?/g

5 match(es)

Price: 19.99, qty -3 and 42 items Order #1001 shipped in 2 boxes
Open in the full Regex Tester (replace, flags, groups) →

How this regex works

PartMeaning
-?optional minus sign
\d+digits
(?:\.\d+)?optional decimal part
g flagfind every match, not just the first

Flags: g finds every match instead of stopping at the first.

Examples

InputMatches
Price: 19.99, qty -3 and 42 items19.99, -3, 42
Order #1001 shipped in 2 boxes1001, 2

Every example above is checked automatically in JavaScript and Python each time the site is built.

Limitations and edge cases

A hyphen is read as a minus sign, so "2026-11-25" gives 2026, -11 and -25. Use \d+ alone if negative numbers never appear.

When to use this pattern

Pulling prices, quantities and IDs out of scraped pages, emails or logs.

Use it in your language

JavaScript

const matches = text.match(/-?\d+(?:\.\d+)?/g) ?? [];

Python

import re
matches = [m.group(0) for m in re.finditer(r'-?\d+(?:\.\d+)?', text)]

PHP

preg_match_all('~-?\\d+(?:\\.\\d+)?~', $text, $m);
$matches = $m[0];

Java

Matcher m = Pattern.compile("-?\\d+(?:\\.\\d+)?").matcher(text);
while (m.find()) { System.out.println(m.group()); }

Go

re := regexp.MustCompile(`-?\d+(?:\.\d+)?`)
matches := re.FindAllString(text, -1)

More regex patterns

Frequently asked questions

What is the regex for Extract numbers from text?
/-?\d+(?:\.\d+)?/g. A hyphen is read as a minus sign, so "2026-11-25" gives 2026, -11 and -25. Use \d+ alone if negative numbers never appear.
Which languages does this pattern work in?
It is tested in JavaScript and Python and uses syntax that also works in PHP (PCRE) and Java. It also works in Go’s RE2 engine.
How do I replace the matches?
In JavaScript use text.replace(/-?\d+(?:\.\d+)?/g, "replacement"); in Python use re.sub(r'-?\d+(?:\.\d+)?', "replacement", text).