Back to functions
Text2026-11-110 related articles

REGEXREPLACE Function in Excel

Replace matching text in a string by using a regular expression pattern.

Syntax

REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])
Table of Contents

Arguments

text

Required

The text or cell reference containing the text you want to search within.

pattern

Required

The PCRE2 regular expression pattern that defines what text to match.

replacement

Required

The text that should replace each matched pattern.

occurrence

Optional

Use 0 or omit to replace all matches, a positive number to count from the start, or a negative number to count from the end.

case_sensitivity

Optional

Controls case-sensitive matching. Use 0 for case-sensitive and 1 for case-insensitive.

What it returns

Returns the updated text after replacing matches defined by the regex pattern.

What REGEXREPLACE does

REGEXREPLACE replaces text inside a string based on a regular expression pattern. It is useful when simple find-and-replace logic is not flexible enough and you need pattern-based cleanup.

Practical examples

Mask the first part of a phone number

Put 212-555-0198 in A2:

=REGEXREPLACE(A2,"^[0-9]+-","***-")
Excel

The result is ***-555-0198. The ^ requires the digit group to begin at the start of this single-line value. Without it, the pattern also matches 555-, and the default all-matches replacement produces ***-***-0198.

Reorder first and last names with capture groups

For this separate example, put MayaLopez in A2:

=REGEXREPLACE(A2,"([A-Z][a-z]+)([A-Z][a-z]+)","$2, $1")
Excel

The result is Lopez, Maya. The parentheses capture Maya and Lopez separately; $2 and $1 put those groups in the opposite order. This example assumes two adjacent names, each starting with one uppercase ASCII letter followed by lowercase ASCII letters. It isn't a general rule for names with spaces, punctuation, or other capitalization.

Common mistakes and notes

REGEXREPLACE is available only in newer Excel builds

Microsoft lists this function only for Excel for Microsoft 365 and Excel for Microsoft 365 for Mac.

Occurrence defaults to all matches

If you omit occurrence, Excel uses 0, which means every match is replaced rather than only the first one.

With A2 set back to 212-555-0198, you can choose a single match without anchoring the pattern:

=REGEXREPLACE(A2,"[0-9]+-","***-",1)
Formula

This returns ***-555-0198. Changing the final argument to -1 replaces the last match and returns 212-***-0198. Positive positions count from the start; negative positions count from the end. Microsoft's REGEXREPLACE reference documents these occurrence rules and the PCRE2 pattern syntax.

Case sensitivity uses numeric flags

By default the match is case-sensitive. Use the optional argument when you want a case-insensitive replacement.

Related functions

Official documentation