Regex to Extract Numbers From Any Text
Pull numbers out of messy text data — prices, measurements, IDs, or any numeric value embedded in strings.
Formula Genius can generate a draft from a plain-English prompt. Supported formulas can be checked with sample cases; review the coverage on the result.
The formula
"Extract all numbers (including decimals) from a text string"
-?\d+\.?\d*
This pattern matches optional negative sign, one or more digits, optional decimal point, and optional decimal digits. Captures integers, decimals, and negative numbers.
Step-by-step breakdown
- -? — optional negative sign
- \\d+ — one or more digits (the integer part)
- \\.? — optional decimal point
- \\d* — zero or more decimal digits
- Combined: matches 42, 3.14, -7, 100.00, etc.
Edge cases & warnings
- Matches partial numbers in dates: "2026-01-15" extracts 2026, -01, -15
- Phone numbers like 555-1234 may extract as separate numbers
- Currency symbols ($, EUR) are not captured — just the number
- Scientific notation (1e5, 2.5E-3) needs a different pattern
Examples
""The price is $42.99 for 3 items""
42.99, 3
""Temperature: -15.5 degrees""
-15.5
Frequently asked questions
How do I extract only integers (no decimals)?
Use \\d+ for integers only. This matches one or more consecutive digits without decimal points.
How do I use this in Google Sheets?
=REGEXEXTRACT(A1, "-?\d+\.?\d*") extracts the first number. For all numbers, use a custom function or REGEXREPLACE to remove non-numeric characters.
Can't find what you need?
Describe a formula in plain English. Formula Genius drafts and explains it, and can check supported formulas on sample cases.