Skip to content

REGEXREPLACE

Replaces the parts of text that match a regular expression with new text.

Syntax

=REGEXREPLACE(, , , , )

TextStatic examples

Excel for Microsoft 365 only, and only on builds that include it. Microsoft has rolled out the REGEX functions in stages by update channel, so on a build without them the formula returns #NAME?. Not available in Excel 2024 or earlier. The practice sheet on this page cannot run REGEXREPLACE yet.

How REGEXREPLACE works

REGEXREPLACE replaces the parts of text that match a regular expression. SUBSTITUTE replaces a fixed piece of text, so removing every non-digit from a phone number needs a nest of calls, one per symbol. REGEXREPLACE does it with a single pattern that describes what to remove.

Availability

REGEXREPLACE is one of three REGEX functions in Microsoft 365, with REGEXTEST and REGEXEXTRACT. Microsoft has released them in stages, so your build may not have them. There, the formula returns #NAME?. They are not in Excel 2024 or earlier. The practice sheet on this page uses Rangewise's own engine, which does not run them yet. The examples show what Excel returns, and the exercise solves the same problems with the older functions.

The arguments

  • text is the text to change.
  • pattern is the regular expression that finds what to replace.
  • replacement is what to put in. An empty string deletes the matches.
  • occurrence is which match to replace. The default 0 replaces them all.
  • case_sensitivity is case-sensitive by default. 1 ignores case.

Common jobs

Keep only digits. The pattern [^0-9] means "any character that isn't a digit", so =REGEXREPLACE(A2,"[^0-9]","") turns Tel: (02) 5550-0142 into 0255500142. With SUBSTITUTE you would need a call for every symbol.

Remove spaces. =REGEXREPLACE(A2,"\s","") removes every space, tab and line break, because \s stands for any whitespace.

Collapse runs of spaces. =REGEXREPLACE(A2,"\s+"," ") turns any run of whitespace into one space. That is the "between words" part of TRIM, without trimming the ends.

Mask digits. =REGEXREPLACE(A2,"\d","#") replaces every digit with a hash, so Order 7719 becomes Order ####.

Replacing only one match

With occurrence, you can pick a match. =REGEXREPLACE("a-b-c","-","/",2) returns a-b/c. A negative number counts from the end, so -1 changes the last match. SUBSTITUTE does the same with its instance_num, but only for fixed text.

Check the pattern first

A pattern can match more than you expect. Test it on a few values and use REGEXTEST to see which rows it matches before you replace anything. Greedy patterns such as .* can swallow far more text than intended.

The result is text

REGEXREPLACE returns text. After removing the symbols from "1,250", you get "1250" as text. Wrap it in VALUE if you need a number.

Before REGEX

Without REGEXREPLACE, you nest SUBSTITUTE calls for fixed pieces of text, use CLEAN and TRIM for whitespace, and fall back on long MID and FIND combinations for patterns. They work in every version of Excel and they are fine for two or three replacements. A pattern is the better tool when the list of characters to remove is long.

Arguments

text
The text to change. Usually a cell such as A2.
pattern
The regular expression that describes what to replace, in quotes, such as "\s+" or "[^0-9]".
replacement
The text to put in place of each match. Use "" to delete the matches.
occurrenceoptional
Which match to replace. 0, the default, replaces all of them. A positive number such as 2 replaces only that match, counting from the start. A negative number counts from the end.
case_sensitivityoptional
0, the default, is case-sensitive. 1 ignores case.

Examples

Example 1

Keep digits only

C2 =REGEXREPLACE(A2,"[^0-9]","")

Result: 0255500142

The pattern matches every character that is not a digit, and the replacement deletes it. The result is text, so the leading zero stays. The practice sheet cannot run this function yet.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 1: Keep digits only
RowABC
1PhoneContactDigits only
2Tel: (02) 5550-0142Front desk0255500142
3
4
5

Example 2

Remove all whitespace

C2 =REGEXREPLACE(A2,"\s","")

Result: 0255500142

\s matches any space, tab or line break, and an empty replacement removes each one.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 2: Remove all whitespace
RowABC
1PhoneContactNo spaces
202 5550 0142Front desk0255500142
3
4
5

Example 3

Collapse repeated spaces into one

C2 =REGEXREPLACE(A2,"\s+"," ")

Result: Anya Kowalski

\s+ matches a run of whitespace, and each run is replaced by a single space.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 3: Collapse repeated spaces into one
RowABC
1Raw nameTeamCleaned
2Anya KowalskiSalesAnya Kowalski
3
4
5

Example 4

Mask every digit

C2 =REGEXREPLACE(A2,"\d","#")

Result: Order ####

Each digit is replaced separately, so four digits give four hashes.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 4: Mask every digit
RowABC
1ReferenceOwnerMasked
2Order 7719FinanceOrder ####
3
4
5

Example 5

Replace only the second match

C2 =REGEXREPLACE(A2,"-","/",2)

Result: PW-24/0113

The fourth argument limits the replacement to the second dash.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 5: Replace only the second match
RowABC
1Batch codeQtyReformatted
2PW-24-0113400PW-24/0113
3
4
5

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

REGEXREPLACE can't run in this sheet yet, so use the older way. In B2, return the phone number in A2 with only its digits, so Tel: (02) 5550-0142 becomes 0255500142. Fill it down to B5.

=SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(SUBSTITUTE(A2,"Tel: ",""),"(",""),")","")," ",""),"-","")

In D2, return the name in C2 with single spaces between words and none at the start or end. Fill it down to D5.

=TRIM(C2)

Common mistakes

  • REGEXREPLACE isn't in every version of Excel. Where it is missing the formula returns #NAME?. Check which version people will use first.
  • A pattern can match more than you intend. Test it on sample values before you fill it down a column.
  • Matching is case-sensitive by default. Set the fifth argument to 1 to ignore case.
  • The result is text. A cleaned number such as "1250" won't add up until you wrap the formula in VALUE.
  • Patterns are not wildcards, and special characters such as ., ( and + mean something in a pattern. To match them literally, put a backslash in front.
  • Every match is replaced by default. To change only one, give the occurrence: 2 for the second match, -1 for the last.

Questions people ask

How do I remove everything except digits from text in Excel?

In Microsoft 365 builds that have it, use `=REGEXREPLACE(A2,"[^0-9]","")`. In other versions, nest SUBSTITUTE calls, one for each symbol you expect.

What is the difference between REGEXREPLACE and SUBSTITUTE?

SUBSTITUTE replaces a fixed piece of text. REGEXREPLACE replaces anything that matches a pattern, such as "any non-digit" or "any run of spaces".

Why does REGEXREPLACE return #NAME?

Your version of Excel doesn't have the function yet. It is Microsoft 365 only and has been released in stages. Use SUBSTITUTE, TRIM and CLEAN instead, or update your build.

How do I replace only the first or last match?

Use the fourth argument: `1` for the first match, `-1` for the last. The default, `0`, replaces every match.

Is REGEXREPLACE case-sensitive?

By default, yes. Add 1 as the fifth argument to ignore case.

Learn REGEXREPLACE in a real sheet.

Tracks take you from the basics to modelling, one graded exercise at a time.