REGEXREPLACE
Swap out parts of a text string that match a regular expression pattern. Great for cleaning data, reformatting values, or stripping unwanted characters.
Spreadsheet editor
Spreadsheet editor
Syntax
=REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])
Returns: Text Arguments
| Argument | Required | Description |
|---|---|---|
| text | Yes | The text you want to change. Type it in quotation marks or point to a cell that holds the text. |
| pattern | Yes | The regular expression that describes what you want to find and replace. Excel uses the PCRE2 style of regex. |
| replacement | Yes | The text to put in place of each match. Use an empty string "" to remove matches, or backreferences like $1 and $2 to reuse the parts you wrapped in parentheses. |
| occurrence | No | Which match to replace. 0 (the default) replaces every match. A positive number counts from the start, so 1 changes the first match. A negative number counts from the end, so -1 changes the last match. |
| case_sensitivity | No | Set to 0 (the default) to treat uppercase and lowercase as different, or 1 to ignore case while matching. |
About
Use it when a plain find-and-replace is not flexible enough. SUBSTITUTE and REPLACE are perfect when you know the exact text or position, but REGEXREPLACE wins when the thing you want to change keeps changing shape, like phone numbers in different formats, extra spaces, or codes that follow a pattern. You can even reuse the matched pieces in your replacement with backreferences such as $1 and $2, which lets you reorder or reformat text on the fly.
REGEXREPLACE works in Microsoft 365 and uses the PCRE2 flavor of regex, the same style you may know from many programming languages. By default it replaces every match, but the occurrence argument lets you target a single match, and case_sensitivity lets you decide whether uppercase and lowercase count as the same. It always returns text, so wrap the result in VALUE when you need a real number. To pull matches out instead of replacing them, use REGEXEXTRACT.
Examples
Reformat with backreferences
Spreadsheet editor
Cleaned text won't add up
Spreadsheet editor
Collapse messy spacing
Spreadsheet editor
Watch out for
It replaces every match by default
Many people expect the formula to change only the first match, but with no occurrence argument REGEXREPLACE swaps every match in the text.
→ Pass an occurrence number to target one match. Use a positive number from the left, like =REGEXREPLACE(A2, "-", " ", 1) for the first dash, or a negative number from the right, like -1 for the last dash.
Backreferences use a dollar sign
Trying to reuse a captured group with \1 or %1 in the replacement does not work, so the result comes back with literal text instead of the match.
→ Refer to capturing groups with a dollar sign and a number, like $1 and $2, for example =REGEXREPLACE(A2, "(\d+)-(\d+)", "$2-$1").
The result is text, not a number
REGEXREPLACE always returns text, so a cleaned-up value like 12345 will not add up or sort as a number.
→ Convert it with VALUE, like =VALUE(REGEXREPLACE(A2, "\D", "")), to get a real number back.
Not available in older Excel
REGEXREPLACE only exists in Microsoft 365, so the formula returns a #NAME? error in Excel 2021 and earlier.
→ On older versions, rebuild the logic with SUBSTITUTE for exact swaps or REPLACE for changes by position.