# REGEXREPLACE function in Excel

> REGEXREPLACE replaces the parts of a text that match a regular expression.

Source: https://spreadsheetpractice.com/functions/regexreplace · Updated October 28, 2026 · Formulas and results on this page were run by the Spreadsheet Practice formula engine on October 30, 2026

## Quick answer

- **What it does:** REGEXREPLACE replaces the parts of a text that match a regular expression.
- **Syntax:** `=REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])`
- **Most common use:** `=REGEXREPLACE(A2,"\D","")` returns 5550142299 (Every non-digit removed)
- **Available in:** Microsoft 365 only
- **Category:** [Text functions](https://spreadsheetpractice.com/functions/category/text)
- **Returns:** The text with the matches replaced.
- **Google Sheets:** Google Sheets has REGEXREPLACE with three arguments: it always replaces every match.

## What it does

REGEXREPLACE replaces the parts of a text that match a regular expression. Use it to strip everything but digits from phone numbers, collapse repeated spaces, or reorder names with captured groups: $1 and $2 put back what each pair of parentheses matched. occurrence picks a single match to replace instead of all of them.

## Syntax

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

| Argument | Required? | What it is |
|---|---|---|
| `text` | Required | The text to change. |
| `pattern` | Required | The regular expression to find. |
| `replacement` | Required | The new text. $1, $2… insert captured groups. |
| `occurrence` | Optional | Optional: 0 (default) replaces every match; 1 the first, 2 the second; negative numbers count from the end. |
| `case_sensitivity` | Optional | Optional: 0 (default) for case-sensitive, 1 to ignore case. |

## Examples

Each result was calculated by running the formula on the sample sheet.

| Cell | Formula | Result | What it shows |
|---|---|---|---|
| B2 | `=REGEXREPLACE(A2,"\D","")` | 5550142299 | Every non-digit removed |
| B3 | `=REGEXREPLACE(A3,"(\w+), (\w+)","$2 $1")` | Ana Lopez | The two captured names swapped |
| B4 | `=REGEXREPLACE(A4," +"," ")` | too many spaces | Runs of spaces become one space |

### The sample sheet

Every formula above runs on this data:

|  | A | B |
|---|---|---|
| 1 | Raw | Result |
| 2 | (555) 014-2299 | 5550142299 |
| 3 | Lopez, Ana | Ana Lopez |
| 4 | too   many    spaces | too many spaces |

## Common mistakes

| What you see | Why, and how to fix it |
|---|---|
| Nothing changes | The pattern didn't match. Check the case (it's case-sensitive by default), and escape special characters such as . ( ) with a backslash. |
| $1 shows up literally | The pattern has no capture group. Put the part you want to keep in parentheses. |
| A number becomes text | The result is always text. Wrap it in VALUE to calculate with it. |

## Tips

- =REGEXREPLACE(A2,"\s+"," ") turns runs of spaces, tabs and line breaks into single spaces.

## Related functions

- [SUBSTITUTE](https://spreadsheetpractice.com/functions/substitute): SUBSTITUTE replaces specific text with new text everywhere it appears, or only at the occurrence you choose.
- [REGEXEXTRACT](https://spreadsheetpractice.com/functions/regexextract): REGEXEXTRACT pulls out the part of a text that matches a regular expression: an order number, an email domain, the digits in a messy code.
- [TRIM](https://spreadsheetpractice.com/functions/trim): TRIM removes spaces from the start and end of text and shrinks runs of spaces between words to one.

## Questions

### How do I remove all non-numeric characters in Excel?

=REGEXREPLACE(A2,"\D","") keeps only the digits. Wrap it in VALUE for a number.

### How do I replace only the first match?

Set occurrence to 1: =REGEXREPLACE(A2,"-","/",1).

## Practice

A graded REGEXREPLACE exercise ("Digits only") is on [the REGEXREPLACE page](https://spreadsheetpractice.com/functions/regexreplace). It is checked instantly, then re-checked on changed data.

---

Spreadsheet Practice (https://spreadsheetpractice.com) teaches spreadsheet formulas by practice: every exercise is graded in a live grid and re-checked on changed data.
