Regex Guide · Prompt: “Match integer or decimal numbers including optional negative signs inside mixed text”
Extract Numbers and Decimals From Text Using Regex
Verified by LiveFormulaLab Engineering Team • Updated November 2026
Extract integers, decimals, and prices from mixed text strings using Regex. Test live with interactive match highlighting.
Verified Regex Syntax
(-?\d+(?:\.\d+)?)How This Formula Works (Step-by-Step)
Mixed log lines and product descriptions frequently embed quantities, weights, or prices inside sentences.
Step 1: -? matches an optional minus sign for negative numbers.
Step 2: \d+(?:\.\d+)? matches one or more digits followed by an optional decimal point and fractional digits.
Formula Syntax & Arguments Breakdown
Matches optional negative signs followed by consecutive integer digits and an optional decimal fraction using non-capturing (?:\.\d+)? so integers, negative deltas, and floating-point amounts extract intact.
| Argument / Clause | Requirement | Data Type | Role & Behavior |
|---|---|---|---|
-? | Optional | Quantifier | Matches an optional leading minus sign for negative deltas, discounts, or temperatures. |
\d+ | Required | Digit Class | Matches one or more consecutive ASCII digits (0–9) forming the whole-number part. |
(?:\.\d+)? | Optional | Non-Capturing Group | Optionally includes a literal decimal point and fractional digits without spawning an extra capture group. |
g / findall flag | Optional | Regex Flag | Enable global matching when a single invoice note or log string contains multiple separate numeric values. |
3 Real-World Use Cases
- Pulling numeric prices or weights from unformatted e-commerce product titles.
- Extracting response times in milliseconds from server log lines.
- Parsing quantity numbers out of invoice memo descriptions.
Cross-Platform Equivalents
- Excel
=VALUE(REGEXEXTRACT(A2, "-?\d+(?:\.\d+)?"))- SQL
SELECT REGEXP_SUBSTR(memo, '-?\d+(\.\d+)?') AS extracted_num FROM logs;- Python Pandas
df['number'] = df['text'].str.extract(r'(-?\d+(?:\.\d+)?)').astype(float)
Common Mistakes & Pro Tips
Pitfalls & How to Fix Them
Mistake: Using \d*\.\d+ misses plain whole integers without decimals; using \d+ alone splits 129.95 into 129 and 95.
Fix: Keep the fractional part optional with \d+(?:\.\d+)? so both integers and decimals extract whole.
Mistake: Strings containing hyphens before words or dates (e.g. "SKU-402") extract "-402" as negative.
Fix: Use (?<![A-Za-z])-?\d+(?:\.\d+)? in engines supporting lookbehinds, or trim prefixes first.
Mistake: Numbers formatted with commas like "$1,250.00" stop extracting at the comma unless commas are accounted for.
Fix: Use (-?\d{1,3}(?:,\d{3})*(?:\.\d+)?) and strip commas before converting to float.
Engineering Pro Tips
- Wrap extracted text strings in VALUE(...) in Excel/Google Sheets or .astype(float) in Pandas before summing.
- Add the g (global) flag in JavaScript or re.findall() in Python when a sentence contains multiple numbers.
- To match currency explicitly, prepend \$\s? before the numeric pattern.
- In Excel 365 with REGEXEXTRACT, pass return_mode = 1 to spill all matched numbers from a single cell into adjacent columns.
Frequently Asked Questions
- How do I match numbers with comma thousands separators like 1,250.50?
- Use (-?\d{1,3}(?:,\d{3})*(?:\.\d+)?) or (-?[\d,]+(?:\.\d+)?).
- What does (?:...) mean in the decimal part?
- (?:...) is a non-capturing group so the decimal portion stays grouped with ? without creating a separate capture group.
- How do I extract all numbers in Python?
- Use re.findall(r"-?\d+(?:\.\d+)?", text).
Related Formulas & Guides
- Validate YYYY-MM-DD ISO Dates with Regular Expressions — Validate and extract strict YYYY-MM-DD calendar dates with Regex, rejecting invalid months (>12) and days (>31). Test live.
- Strong Password Validation Regex Using Lookaheads — Enforce strong password complexity with positive lookaheads in Regex (8+ characters, uppercase, lowercase, digit, symbol). Test live.
- Extract Domain and Path From URLs Using Regex — Parse scheme, domain hostname, and path from HTTP/HTTPS URLs using a clean Regex pattern with capture groups. Test interactively live.
- Match and Normalize US Phone Numbers with Regex — Match and normalize US/North American phone numbers formatted with parentheses, hyphens, dots, or +1 country code using Regex live.