The REGEXREPLACE function in Excel replaces the parts of a text that match a regular expression: =REGEXREPLACE(A2,"\d","") removes all digits from a cell, =REGEXREPLACE(A2,"\s+"," ") collapses repeated spaces, and =REGEXREPLACE(A2,"(\S+) (\S+)","$2 $1") swaps two words. Unlike SUBSTITUTE, it replaces not a specific string but anything that fits the pattern, and it can rearrange the parts of the match with $1, $2. The function is available in Excel for Microsoft 365.
| Function category | Text functions |
| Excel versions | Excel for Microsoft 365 (Windows and Mac) |
| Volatility | Non-volatile |
| Similar functions | REGEXTEST, REGEXEXTRACT, SUBSTITUTE, REPLACE |
REGEXREPLACE function syntax
=REGEXREPLACE(text,pattern,replacement,[occurrence],[case_sensitivity])
- text — the cell or string to replace in;
- pattern — the regular expression describing what to replace;
- replacement — the new text; an empty string "" deletes the match, $1, $2… insert the groups in parentheses;
- occurrence — optional: 0 (default) — replace all matches, 2 — only the second, −1 — only the last;
- case_sensitivity — optional: 0 (default) — case-sensitive, 1 — case-insensitive.
Patterns use the PCRE2 syntax, the same as in REGEXTEST and REGEXEXTRACT. Tokens and quantifiers are explained in the article on regular expressions in Excel.
Remove characters by pattern
To delete fragments, use an empty replacement "". One formula removes all digits, all letters, punctuation or everything except the characters you need.
| A | B | C | |
|---|---|---|---|
| 1 | Text | We remove | Result |
| 2 | USB-C cable 2m, SKU 48213! | all digits | USB-C cable m, SKU !=REGEXREPLACE(A2,"\d","") |
| 3 | USB-C cable 2m, SKU 48213! | all letters | - 2, 48213!=REGEXREPLACE(A3,"[A-Za-z]","") |
| 4 | USB-C cable 2m, SKU 48213! | everything except digits | 248213=REGEXREPLACE(A4,"\D","") |
| 5 | USB-C cable 2m, SKU 48213! | punctuation | USBC cable 2m SKU 48213=REGEXREPLACE(A5,"[^\sA-Za-z0-9]","") |
\D is any character except a digit, so only digits remain. [^\sA-Za-z0-9] is anything that isn’t a space, letter or digit: that removes the hyphen, comma and exclamation mark. Double spaces may remain after deleting — clean them with TRIM.
Spaces and junk in data
Regular expressions are great for cleaning exports: they collapse spaces of any length, remove them only at the start or end, or replace line breaks.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Text | Task | Result | Length |
| 2 | Seattle , Pine St | all spaces → one | Seattle , Pine St =REGEXREPLACE(A2,"\s+"," ") | 19=LEN(C2) |
| 3 | Seattle , Pine St | only at the edges | Seattle , Pine St=REGEXREPLACE(A3,"^\s+|\s+$","") | 20=LEN(C3) |
| 4 | Seattle , Pine St | space before a comma | Seattle, Pine St =REGEXREPLACE(A4,"\s+,",",") | 24=LEN(C4) |
The original string is 25 characters long. \s+ catches any whitespace in a row, including tabs and line breaks; a single space remains at each edge. ^\s+|\s+$ means “spaces at the start OR at the end”, and spaces between words stay as they are.
Replace only the Nth or the last occurrence
The fourth argument chooses which match to replace. A positive number counts from the start of the string, a negative one from the end. SUBSTITUTE can also replace the Nth instance, but only of a specific string and only from the start.
| A | B | C | |
|---|---|---|---|
| 1 | Text | Occurrence | Result |
| 2 | 2026-10-22-report-final | 0 — all | 2026_10_22_report_final=REGEXREPLACE(A2,"-","_",0) |
| 3 | 2026-10-22-report-final | 1 — first | 2026_10-22-report-final=REGEXREPLACE(A3,"-","_",1) |
| 4 | 2026-10-22-report-final | 2 — second | 2026-10_22-report-final=REGEXREPLACE(A4,"-","_",2) |
| 5 | 2026-10-22-report-final | -1 — last | 2026-10-22-report_final=REGEXREPLACE(A5,"-","_",-1) |
There are four hyphens in the string. 0 replaces all, 2 only the second, −1 the last one, between “report” and “final”.
Rearrange and reformat: groups $1, $2
Parts of the pattern in parentheses are remembered, and the replacement refers to them as $1, $2 and so on. That’s how you change word order, date or phone format without helper columns.
| A | B | C | |
|---|---|---|---|
| 1 | Original | Task | Result |
| 2 | Smith John | swap last and first name | John Smith=REGEXREPLACE(A2,"(\S+) (\S+)","$2 $1") |
| 3 | 10/22/2026 | MM/DD/YYYY → YYYY-MM-DD | 2026-10-22=REGEXREPLACE(A3,"(\d{2})/(\d{2})/(\d{4})","$3-$1-$2") |
| 4 | 2065550142 | phone by mask | (206) 555-0142=REGEXREPLACE(A4,"(\d{3})(\d{3})(\d{4})","($1) $2-$3") |
| 5 | john.smith@mail.com | hide part of an email | jo***@mail.com=REGEXREPLACE(A5,"^([^@]{2})[^@]*","$1***") |
The date result is the text “2026-10-22”; Excel usually recognizes this ISO format as a date when you paste values. In the last row the first two characters of the name stay, the rest before @ becomes asterisks.
Mask data: lookahead
Lookahead (?=…) replaces a character only if something follows it, without touching that “tail”. The classic example is hiding a card or account number except for the last four digits.
| A | B | |
|---|---|---|
| 1 | Number | Masked |
| 2 | 4276 1234 5678 9012 | **** **** **** 9012=REGEXREPLACE(A2,"\d(?=(?:\D*\d){4})","*") |
| 3 | 2065550142 | ******0142=REGEXREPLACE(A3,"\d(?=(?:\D*\d){4})","*") |
\d(?=(?:\D*\d){4}) means “a digit followed later in the string by at least four more digits”. Spaces and hyphens are skipped, so the number format is preserved.
Case-insensitive replacement
Matching is case-sensitive by default: the pattern “inc” won’t find “INC”. The fifth argument 1 turns case sensitivity off; if you don’t need a specific occurrence, set the fourth argument to 0.
| A | B | C | |
|---|---|---|---|
| 1 | Text | Formula | Result |
| 2 | Acme INC, Globex inc, Initech Inc | case-sensitive | Acme INC, Globex, Initech Inc=REGEXREPLACE(A2," inc","") |
| 3 | Acme INC, Globex inc, Initech Inc | case-insensitive | Acme, Globex, Initech=REGEXREPLACE(A3," inc","",0,1) |
Case-sensitively only “inc” is removed, case-insensitively all three spellings.
If your Excel doesn’t have REGEXREPLACE
In Excel 2010–2024 without a Microsoft 365 subscription, the free !SEMTools add-in adds its own REGEXREPLACE function with the same pattern syntax and $1, $2 groups: =REGEXREPLACE(A2,"\d",""). In Excel for Microsoft 365 the built-in function with the same name takes priority. The add-in version has different optional arguments: the fourth is FALSE to replace only the first match, the fifth is TRUE to ignore case. It can’t target the Nth or last occurrence, and the VBScript engine doesn’t support lookbehind (?<=…).
Why REGEXREPLACE doesn’t work as expected: common errors
- #NAME?. The function isn’t available in your Excel version — install !SEMTools to get REGEXREPLACE.
- More is replaced than you wanted. A dot means any character, and * and + are greedy. Escape special characters (\.) and use lazy quantifiers (.*?).
- $1 is inserted as the literal text “$1”. The pattern has no parentheses, or the group number is larger than the number of groups.
- A number turned into text. REGEXREPLACE always returns text; add -- before the formula to calculate.
- Nothing is replaced in a different case. Set the fifth argument to 1.
REGEXREPLACE function — FAQ
What does the REGEXREPLACE function do in Excel?
It replaces all fragments of text that match a regular expression: =REGEXREPLACE(A2,"\d","") removes all digits.
How is REGEXREPLACE different from SUBSTITUTE?
SUBSTITUTE replaces a specific string; REGEXREPLACE replaces anything that fits a pattern — any digits, words of a certain shape, spaces of any length. It can also rearrange parts of the match with $1, $2.
How do I replace only the last occurrence?
Set the fourth argument to −1: =REGEXREPLACE(A2,"-","_",-1).
How do I keep only digits in a cell?
Remove everything except digits: =REGEXREPLACE(A2,"\D","").
What is REGEXREPLACE called in Russian Excel?
РЕГЗАМЕНИТЬ.
Delete or replace characters, words and fragments by regular expression right in the cells, without helper columns — in a couple of clicks with !SEMTools, in any Excel version.
!SEMTools solves this and hundreds of other tasks in a couple of clicks!
Summary: the REGEXREPLACE function in Excel
| Task | Formula |
|---|---|
| Remove digits | =REGEXREPLACE(A2,"\d","") |
| Keep only digits | =REGEXREPLACE(A2,"\D","") |
| Collapse spaces | =REGEXREPLACE(A2,"\s+"," ") |
| Last occurrence | =REGEXREPLACE(A2,"-","_",-1) |
| Swap two words | =REGEXREPLACE(A2,"(\S+) (\S+)","$2 $1") |
I hope you found what you were looking for on this page. REGEXREPLACE replaces chains of nested SUBSTITUTE with one formula: describe what to find — and say what to replace it with.
Articles that use REGEXREPLACE
- Remove last N characters (from right) in Excel
- Regular expressions in Excel | Find, extract and replace text
- Working with AI in Excel
- Slug generator in Excel: SEO-friendly URLs
- How to find and replace multiple values in Excel in bulk
- Remove first N characters (from left) in Excel cells
- Web scraping in Excel
- Remove Punctuation in Excel — Keep Only Letters and Numbers
- Bulk edit hyperlinks in Excel
- Delete the last word in Excel cells / delete the last N words