Text function

REGEXREPLACE function

Replaces text that matches a regular expression with new text, with support for capture-group references like $1.

Available in Microsoft 365 only (Windows, Mac and Excel for the web; rolled out from 2024)

Microsoft 365 onlyDifferent in Google Sheets

Syntax

REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])
ArgumentWhat it means
textRequiredThe text or cell to change.
patternRequiredThe regular expression to look for.
replacementRequiredThe text to put in place of each match. $1, $2 ... insert what the capture groups matched.
occurrenceOptionalWhich match to replace: 0 (default) all of them, 2 only the second, -1 only the last, and so on.
case_sensitivityOptional0 (default) case-sensitive; 1 ignore case.

Returns: The text with the matches replaced.

How REGEXREPLACE works

REGEXREPLACE is SUBSTITUTE with super-powers. SUBSTITUTE can only swap one exact piece of text for another; REGEXREPLACE swaps anything that fits a pattern. With one formula you can strip every non-digit from a phone number, collapse any run of spaces, remove everything in brackets, or mask all but the last digits of an account number.

Capture groups make it possible to rearrange text: brackets in the pattern remember what they matched and $1, $2 put those pieces back in any order, so Silva, Ana can become Ana Silva. Quick pattern guide: \d a digit, \w a letter, digit or underscore, \s whitespace, . any character, [A-Z] one capital letter, [^0-9] anything except a digit, + one or more, * zero or more, {4} exactly four, ^ start of text, $ end of text, ( ) a capture group, | or.

By default every match is replaced. Use occurrence to target just one (counting from the end with negative numbers). If nothing matches, the original text is returned unchanged - no error.

Examples

1Keep only the digits of a phone number

B2fx=REGEXREPLACE(A2,"\D","")
AB
1Phone
2+30 (210) 555-0147302105550147

Result: 302105550147

\D matches any character that is not a digit; replacing each with nothing leaves only the digits.

2Swap "Last, First" to "First Last"

B2fx=REGEXREPLACE(A2,"^(\w+), (\w+)$","$2 $1")
AB
1Name (CRM export)
2Silva, AnaAna Silva

Result: Ana Silva

Group 1 captures Silva, group 2 captures Ana. The replacement writes them back in the opposite order.

3Collapse runs of spaces

B2fx=REGEXREPLACE(A2,"\s+"," ")
AB
1Product
2Laptop Stand ProLaptop Stand Pro

Result: Laptop Stand Pro

\s+ matches any run of whitespace (spaces, tabs, line breaks) and replaces it with a single space.

4Mask an account number

B2fx=REGEXREPLACE(A2,"\d(?=\d{3})","*")
AB
1Account
2ACC-90817263ACC-*****263

Result: ACC-*****263

The lookahead (?=\d{3}) matches a digit only when at least three more digits follow, so every digit except the last three becomes *.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 17 KB

Common errors and how to fix them

ErrorWhy it happensFix
#VALUE!The pattern is invalid, e.g. an unclosed bracket or a stray * at the start.Fix the pattern; escape special characters with \.
#NAME?Your Excel does not have the REGEX functions.Use Microsoft 365, or SUBSTITUTE for simple exact replacements.

Tips

  • To insert a literal dollar sign in the replacement, write $$.
  • Use case_sensitivity 1 to replace vat, VAT and Vat in one go.
  • Wrap the result in TRIM if removing words leaves double spaces behind.

Questions

How do I refer to a captured group in the replacement?

Use $1 for the first bracketed group, $2 for the second and so on. $0 inserts the whole match.

What is the difference between REGEXREPLACE and SUBSTITUTE?

SUBSTITUTE replaces one exact text and works in every Excel version. REGEXREPLACE replaces anything matching a pattern but needs Microsoft 365.

Google Sheets

Exists as REGEXREPLACE(text, regular_expression, replacement) using RE2 syntax; it always replaces every match and has no occurrence or case_sensitivity arguments.

REGEXREPLACE in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
FinnishREGEXKORVAA
PolishREGEXZASTĄP
Portuguese (Portugal)REGEX.SUBSTITUIR
RussianРЕГЗАМЕНИТЬ
SpanishREGEXREEMPLAZAR