Write Google Sheets REGEX Formulas to Extract and Clean Text

Get ready-to-paste REGEXEXTRACT, REGEXREPLACE, or REGEXMATCH formulas for Google Sheets with a token-by-token breakdown.

Text prompt for Gemini and ChatGPT

You get: Formula, how It Works and edge Cases

Prompt

Prompt
Act as a Google Sheets formula specialist. I will paste 3 to 5 sample text strings and tell you whether I need to extract, replace, or match a pattern.

Write the exact Google Sheets formula using REGEXEXTRACT, REGEXREPLACE, or REGEXMATCH. Follow these rules:
1. Use RE2-compatible regular expression syntax, because Google Sheets does not support lookbehinds or backreferences.
2. Wrap the formula in IFERROR so non-matching cells return a blank string instead of an error.
3. Keep the pattern as simple and readable as possible while handling all provided edge cases.
4. Assume the input text is in cell A2 unless I specify another cell.

Output format:
- **Formula:** The complete, ready-to-paste Google Sheets formula.
- **How It Works:** A token-by-token breakdown of the regular expression in a short bulleted list.
- **Edge Cases:** One or two variations that could break the pattern and how to adjust for them.

End with one line stating any assumptions made about spaces, capitalization, or missing characters.

Example output

How the REGEX formula extracts an order IDToken-by-token breakdown of the example formula
  1. ORD-PrefixMatches the literal prefix before every order number
  2. (...)GroupCapturing group returns only the digits, not the prefix
  3. [0-9]+DigitsMatches one or more consecutive digits
  4. IFERROR(..., "")FallbackReturns an empty string if no order ID matches

Example for: extracting numeric order IDs from messy customer support subject lines in cell A2.

  • Formula:
    =IFERROR(REGEXEXTRACT(A2, "ORD-([0-9]+)"), "")
  • How It Works:

    • ORD- matches the literal prefix before every order number.
    • (...) creates a capturing group so REGEXEXTRACT returns only the digits inside, not the prefix.
    • [0-9]+ matches one or more consecutive digits.
    • IFERROR(..., "") returns an empty string if a row has no matching order ID.
  • Edge Cases:

    • Lowercase prefixes like ord-482910 will not match. Add (?i) at the start of the pattern ("(?i)ORD-([0-9]+)") to make it case-insensitive.
    • If an order ID contains a hyphenated suffix like ORD-482910-B, this formula only grabs 482910.

Assumptions: Order IDs always start with uppercase ORD- followed immediately by digits with no spaces.

Written by AI from this prompt. Your results will vary.

Tips and common edits

Why this works

  • Enforces RE2 syntax so the generated pattern never uses unsupported lookbehinds.
  • Wraps formulas in IFERROR to keep messy columns free of #N/A errors.
  • Includes a token-by-token breakdown so you can tweak the pattern yourself later.

Paste 3 to 5 sample strings and describe what you want to extract, replace, or match. Works in Gemini, ChatGPT, or Claude.

Common edits

  • Ask for an ARRAYFORMULA wrapper if you want one formula to process an entire column.
  • Specify case-insensitivity if your source text mixes uppercase and lowercase letters.

Reuse it: Use in a workflowTurn into a skillAdapt in the builder

Excel & Spreadsheets

Deduplicate spreadsheet rows keeping latest entry

Finds duplicate records in your data and generates clean formulas or step-by-step instructions to keep only the newest record.

Excel & Spreadsheets

Clean phone numbers and postal codes using Excel formulas

Transform irregular contact details into uniform formats for clean CRM imports. Use this whenever raw exports mix dashes, dots, spaces, or leading zeroes.

Excel & Spreadsheets

Parse Unstructured Text into Clean Excel Columns

This prompt generates specific Excel formulas to split messy text strings into organized columns while handling edge cases like missing delimiters.

Excel & Spreadsheets

Clean and Standardize Messy Spreadsheet Data with Exact Formulas

Get a column-by-column cleaning plan with ready-to-fill formulas for spaces, case, dates and duplicates, plus a list of rows that need a manual check.

Excel & Spreadsheets

Explain Any Excel or Google Sheets Formula Step by Step in Plain English

Paste a formula and get a plain-language walkthrough in evaluation order, a table of what each part returns, common ways it breaks with fixes, and a simpler version.

Excel & Spreadsheets

Pick XLOOKUP, VLOOKUP or INDEX MATCH and Get the Exact Formula

You get one recommended lookup method for your Excel version and data layout, a paste-ready formula, and clear handling for values that have no match.