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

Regex to Extract Numbers from a String

Copy the pattern, paste your text below, and see every number it extracts. Below the tester: how it handles decimals, signs, and thousands separators.

Regex pattern

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

This matches every integer or decimal number in a string, including a leading minus sign. Use the global flag to pull out all numbers at once; it ignores thousands separators.

/ /

What this pattern matches

This extracts every number in the text: whole numbers, decimals, and negative numbers. Use it with a global flag to collect them all.

How it works, part by part

-? — an optional leading minus sign.

\d+ — one or more digits (the integer part).

(?:\.\d+)? — an optional decimal point and fractional digits.

Thousands separators

This treats 1,234 as two numbers (1 and 234). To keep grouped numbers whole, use -?\d{1,3}(?:,\d{3})*(?:\.\d+)? instead.

Use it in your code

Python
import re
text = "Order 12 units at $3.50 each, save -2 returns, total was 1234 items."
pattern = r'''-?\d+(?:\.\d+)?'''
for m in re.finditer(pattern, text):
    print(m.group())
JavaScript
const text = "Order 12 units at $3.50 each, save -2 returns, total was 1234 items.";
const re = new RegExp(String.raw`-?\d+(?:\.\d+)?`, "g");
console.log(text.match(re));

FAQ

Does it capture decimals?

Yes. 3.50 is matched as one number thanks to the optional decimal group.

Does it handle negative numbers?

Yes. The optional leading - captures -2 as a negative number.

Why does 1,234 split into two?

The base pattern does not know about thousands separators. Use the grouped variant shown above to keep it whole.