Quick Answer

REGEXREPLACE Function

REGEXREPLACE searches text for a pattern and replaces matching parts with new text.

✓ Excel✓ Google SheetsExcel Google Sheets only

Syntax

REGEXREPLACE
(text, regular_expression, replacement)

Parameters

ParameterDescriptionRequired
textThe original text or cell reference.Required
regular_expressionThe RE2 pattern to find in the text.Required
replacementThe text to replace matches with. Use $1, $2, etc. to refer to capture groups.Required

Basic Example

Remove all digits from a product code

=REGEXREPLACE('ABC-123-XYZ', '\d', '')
ResultABC--XYZ

The pattern \d matches any digit. Replacing all digits with an empty string removes them.

Advanced Examples

Example 1: Reformat a phone number

Data cleaning

Convert '(555) 123-4567' to '555-123-4567'

=REGEXREPLACE(A2, '\((\d{3})\)\s*(\d{3})-(\d{4})', '$1-$2-$3')
Result: 555-123-4567
Capture groups grab the area code, prefix, and line number. The replacement string uses $1, $2, $3.

Example 2: Remove extra whitespace

Text cleanup

Replace multiple consecutive spaces with a single space

=REGEXREPLACE(A2, '\s+', ' ')
Result: Text with single spaces
\s+ matches one or more whitespace characters. Replacing with a single space normalizes spacing.

How REGEXREPLACE Works

REGEXREPLACE scans the text for all matches of the pattern and replaces each match with the replacement string. The replacement can include references to capture groups using $1, $2, etc. It uses RE2 regex syntax.

1
Select the text
Choose the cell or string to modify.
2
Define the pattern
Write a regular expression matching the text to replace.
3
Write the replacement
Use literal text or $1, $2 for capture groups.
4
Enter the formula
Type =REGEXREPLACE(text, 'pattern', 'replacement').

Important Notes & Limitations

  • REGEXREPLACE is Google Sheets only. Excel has no built-in regex replacement function.

  • It uses RE2 syntax, which lacks some advanced regex features like lookahead/lookbehind.

  • Replacement is global; there is no built-in option to replace only the first match.

  • Special characters in the replacement string may need escaping.

Common Errors & Fixes

#VALUE! errorThe regular expression is invalid or uses unsupported syntax.

Fix: Validate the regex and use RE2-compatible patterns.

Replacement not appliedThe pattern does not match the text.

Fix: Test the pattern with REGEXMATCH first to ensure it matches.

$1 not replacedCapture groups are not defined or the replacement syntax is wrong.

Fix: Use parentheses in the pattern and $1, $2 in the replacement string.

Download Practice File

Practice REGEXREPLACE with Real Data

Download a sample CSV file with pre-populated data and practice exercises for the REGEXREPLACE function. Works in both Excel and Google Sheets.

Works in Google SheetsCompatible with ExcelIncludes exercises

File format: CSV (comma-separated values) - opens in Excel, Google Sheets, and all spreadsheet apps

Frequently Asked Questions

Can REGEXREPLACE replace only the first match?
No, it replaces all matches by default. To replace only the first match, you need a workaround or script.
Can I use REGEXREPLACE in Excel?
No, Excel has no built-in regex replacement. Use SUBSTITUTE for fixed text or VBA/Power Query for patterns.
How do I reference capture groups in the replacement?
Use $1, $2, etc. For a literal dollar sign, use $$.
Can I use REGEXREPLACE with ARRAYFORMULA?
Yes, wrap it to apply the replacement to a whole range at once.