Contact

REGEXREPLACE in Excel: Replace Patterns and Reuse Capture Groups

Text Intermediate Microsoft Excel

Replace matching patterns in Excel with REGEXREPLACE, control occurrences and case, and rebuild structured codes using capture groups.

Verification status: See the evidence and test scope described in this guide. How formula examples are checked

Quick answer: REGEXREPLACE replaces text matching a regular-expression pattern. Excel can replace all matches, one chosen occurrence or a match counted from the end, and can insert captured parts into the replacement.

Availability and version history

  • Introduced: Preview announced May 20, 2024: Windows Beta 2406 (17715.20000), Mac 16.86 (24051422). These are announced preview minima. Microsoft announcement.
  • Availability: official help lists Excel for Microsoft 365, Excel for Microsoft 365 for Mac.
  • Checked: October 14, 2026; official REGEXREPLACE reference. Documentation check; examples independently calculated, not native-tested.

Syntax and defaults

=REGEXREPLACE(text,pattern,replacement,[occurrence],[case_sensitivity])

The first three arguments are required. Omitted occurrence is 0, meaning all matches; a positive number selects a match from the start, while a negative number selects from the end. Matching is case-sensitive by default (case_sensitivity=0); 1 enables case-insensitive matching. Excel documents the PCRE2 regex flavor and $1, $2 and similar references for captured groups in replacement text. REGEXREPLACE reference

Use Excel’s formula quoting, not the escape rules of another programming language. A backslash inside the quoted regex is a regex character; these worksheet examples do not require doubling it as Python source code might.

Example 1: replace runs of digits

Put this fictional string in A2:

Batch 17 contains 240 units

Then enter:

=REGEXREPLACE(A2,"[0-9]+","#")

Expected result: Batch # contains # units. The character class selects digits, and the plus sign makes each consecutive run one match. Without +, each individual digit would be matched separately, producing a different number of replacement marks.

This transforms every numeric run, so it is too broad if some numbers must remain. Define the intended pattern before applying it to production data. Replacing a few visible fragments is also not a guarantee that a text has been fully anonymized.

Example 2: choose a particular occurrence

For the same A2:

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

Expected results are Batch # contains 240 units and Batch 17 contains # units. The first formula changes the first numeric run; the second changes the last.

The occurrence counts regex matches, not individual characters and not worksheet rows. Here each multi-digit number is one match because the pattern includes +.

Example 3: make case sensitivity explicit

Put cat CAT catalog cat in A4. Use word boundaries so the letters inside catalog are not replaced:

=REGEXREPLACE(A4,"\bcat\b","dog")

Expected result: dog CAT catalog dog. The uppercase CAT remains because matching is case-sensitive.

=REGEXREPLACE(A4,"\bcat\b","dog",0,1)

Expected result: dog dog catalog dog. The fourth argument explicitly selects all occurrences, while the fifth changes case sensitivity. Do not put 1 in the fourth position and assume it means case-insensitive matching.

These ASCII examples avoid assuming that every language or Unicode word-boundary case behaves the same way. Test the exact character set required by a real dataset.

Advanced example: retain a captured identifier

Put ORD-0017 / ORD-0082 in A6:

=REGEXREPLACE(A6,"ORD-([0-9]{4})","ID-$1")

Expected result: ID-0017 / ID-0082. Parentheses capture exactly four digits; $1 inserts those captured characters into each replacement. The leading zeros remain because they are text characters, not numbers being reformatted.

For a stricter entire-cell conversion, use anchors:

=REGEXREPLACE("ORD-0017","^ORD-([0-9]{4})$","ID-$1")

Expected result: ID-0017. The anchors restrict the match to a whole string of that shape. They would prevent a matching fragment in a longer description from being replaced by this specific pattern.

Reorder two captured parts

=REGEXREPLACE("A17-B204","^([A-Z][0-9]+)-([A-Z][0-9]+)$","$2/$1")

Expected result: B204/A17. The first capture is A17; the second is B204. The replacement reverses their order and inserts a slash. This is an original code-format example, not a general parser for all possible identifiers.

Troubleshooting and safer transformations

A pattern containing punctuation may need regex escaping. For instance, . is a wildcard token rather than an ordinary literal period. Test a minimal positive case and a nearby nonmatching case before widening the pattern.

Keep the original text in a separate cell or column while checking the transformed output. Compare the occurrence and case arguments individually when only some matches change. Do not silently convert the result to a number if leading zeros or letters are meaningful.

Google Sheets also has a REGEXREPLACE function, but its regex flavor and function signature differ. A shared function name does not make these five-argument PCRE2 examples directly portable. For simple literal replacements, SUBSTITUTE may communicate the intention more clearly.

Verification: These are fictional inputs and independently calculated expected results, not native Excel observations. See the editorial testing standards.