Pattern library / Extract & Replace
Regex to Extract Numbers from a String
Almost every data-cleanup task hits the same wall: text like "total: 1,249.50 USD" where you need 1249.50. Regex solves it by matching the number shape and capturing it — the patterns below scale from “any integer” to “signed decimal with separators”, each with the exact use case it fits.
How the pattern works
[-+]?— optional sign, so negatives and explicit positives survive.[0-9]{1,3}(,[0-9]{3})*— thousands-separated groups (1,249); drop this part for raw digits.(\.[0-9]+)?— optional decimal part.- Simpler variants:
[0-9]+(any integer run),-?[0-9]+(signed int),[0-9]+\.[0-9]+(decimal). The main pattern is the careful one; use the smallest variant that fits your data. - Global flag (
g) extracts every number; without it you get only the first.
Test it: match & no-match examples
Every row is loaded straight into the tester — click Try to see the match live:
| Input | Result | Why | |
|---|---|---|---|
order total: 1,249.50 USD | Match | thousands + decimal captured | Try → |
-42 | Match | signed integer | Try → |
temp +98.6F | Match | explicit plus sign | Try → |
v1.2.3 release | Match | matches 1.2 then .3 — see pitfalls | Try → |
no digits here | No match | nothing to extract | Try → |
1234 | Match | plain 4-digit int (groups optional) | Try → |
Where to use it
JavaScript (capture all)
const s = 'total: 1,249.50 USD, tax 124.50';
const nums = s.match(/[-+]?[0-9]{1,3}(?:,[0-9]{3})*(?:\.[0-9]+)?/g)
.map(x => Number(x.replace(/,/g, ''))); // [1249.5, 124.5]
Python (findall + clean)
import re
nums = re.findall(r'[-+]?[0-9]{1,3}(?:,[0-9]{3})*(?:\.[0-9]+)?', 'total: 1,249.50 USD')
values = [float(n.replace(',', '')) for n in nums if n]
Bash (strip everything but numbers)
echo 'total: 1,249.50 USD' | grep -oE '[-+]?[0-9]{1,3}(,[0-9]{3})*(\.[0-9]+)?'
Common mistakes
v1.2.3matches as1.2then3— version strings need the semver pattern, not a number pattern.- European format (
1.249,50) swaps separators — mirror them:[0-9]{1,3}(\.[0-9]{3})*,[0-9]+. - Bare
\d+also grabs digits inside words (order2) — add boundaries\bwhen that matters.
FAQ
How do I extract only whole numbers?
Use [-+]?\b[0-9]+\b — it ignores decimal fragments and avoids digits glued to letters.
How do I convert the extracted text to a number?
Strip separators first: Number(x.replace(/,/g, '')) in JS, float(x.replace(',', '')) in Python. Regex finds the shape; parsing makes it a value.
Why do I get [''] in my matches?
With the global flag, optional groups can match empty strings at every position. Filter empties: .filter(Boolean) in JS or [n for n in nums if n] in Python.
Related patterns
Extract Domain from URL — Regex Capture Group · Remove Query Parameters with Regex (Replace Example) · IPv4 Address Regex — Validate Dots & Octets
For GA4-specific patterns, see the GA4 regex cheat sheet, the page path regex guide, the event filter regex guide, the GA4 regex match types, the custom channel grouping guide and RE2 vs PCRE. Browse the full regex pattern library.