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 / ClauseRequirementData TypeRole & Behavior
-?OptionalQuantifierMatches an optional leading minus sign for negative deltas, discounts, or temperatures.
\d+RequiredDigit ClassMatches one or more consecutive ASCII digits (0–9) forming the whole-number part.
(?:\.\d+)?OptionalNon-Capturing GroupOptionally includes a literal decimal point and fractional digits without spawning an extra capture group.
g / findall flagOptionalRegex FlagEnable 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