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])| Argument | What it means | |
|---|---|---|
text | Required | The text or cell to change. |
pattern | Required | The regular expression to look for. |
replacement | Required | The text to put in place of each match. $1, $2 ... insert what the capture groups matched. |
occurrence | Optional | Which match to replace: 0 (default) all of them, 2 only the second, -1 only the last, and so on. |
case_sensitivity | Optional | 0 (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
| A | B | |
|---|---|---|
| 1 | Phone | |
| 2 | +30 (210) 555-0147 | 302105550147 |
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"
| A | B | |
|---|---|---|
| 1 | Name (CRM export) | |
| 2 | Silva, Ana | Ana 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
| A | B | |
|---|---|---|
| 1 | Product | |
| 2 | Laptop Stand Pro | Laptop 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
| A | B | |
|---|---|---|
| 1 | Account | |
| 2 | ACC-90817263 | ACC-*****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 *.
Common errors and how to fix them
| Error | Why it happens | Fix |
|---|---|---|
#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_sensitivity1 to replacevat,VATandVatin 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.
| Language | Function name |
|---|---|
| Finnish | REGEXKORVAA |
| Polish | REGEXZASTĄP |
| Portuguese (Portugal) | REGEX.SUBSTITUIR |
| Russian | РЕГЗАМЕНИТЬ |
| Spanish | REGEXREEMPLAZAR |