The direct answer: in Google Sheets, use the REGEXREPLACE function to find and replace text using regular expressions. The syntax is REGEXREPLACE(text, regular_expression, replacement) — for example, =REGEXREPLACE(A1, "\d+", "#") replaces every run of digits in A1 with a hash symbol.
What Is REGEXREPLACE and Why Use It?
Regular expressions (regex) are a pattern-matching language that lets you describe text patterns rather than exact strings. Unlike the simple SUBSTITUTE function, which replaces only literal text, REGEXREEXPLACE can match variable patterns like phone numbers, email addresses, or repeated spaces. This makes it invaluable for cleaning messy data, extracting parts of strings, or standardizing formats across hundreds of rows.
For instance, if you have a column of product codes like “SKU-1234-XYZ” and you want to remove everything except the digits, you can use =REGEXREPLACE(A2, "[^0-9]", "") to strip all non-digit characters. The function is part of Google Sheets’ trio of regex functions: REGEXEXTRACT, REGEXMATCH, and REGEXREPLACE.
How REGEXREPLACE Works: Syntax and Arguments
The function takes three arguments, all required:
- text — the cell reference or string you want to modify.
- regular_expression — the pattern to match. It must be in quotes and uses RE2 syntax (the regex engine Google Sheets uses).
- replacement — the text that replaces the matched portions. You can also use capture groups in the replacement (see below).
Here’s a concrete example. Suppose cell A1 contains “Order #12345 shipped on 2024-03-15”. To replace the date with the word “DATE”, you’d write:
=REGEXREPLACE(A1, "\d{4}-\d{2}-\d{2}", "DATE")
This returns “Order #12345 shipped on DATE”. Note that the backslash before d is doubled in the formula because Google Sheets uses the backslash as an escape character in strings; in the regex itself, \d means any digit.
Using Capture Groups in the Replacement
One of the most powerful features is capturing parts of the match and reusing them in the replacement. You enclose parts of the pattern in parentheses to create groups, then refer to them with $1, $2, etc. in the replacement text.
Example: you have names in the format “Last, First” in A1 (e.g., “Doe, John”). To flip it to “First Last”, use:
=REGEXREPLACE(A1, "(\w+), (\w+)", "$2 $1")
This returns “John Doe”. The pattern matches one or more word characters, a comma and space, then more word characters; the two groups are then reordered.
Common Regex Patterns for Data Cleaning
Here are practical patterns you’ll often use in REGEXREPLACE:
- Remove all non-digits:
[^0-9]— replaces every character that is not a digit with the replacement (often empty string “”). - Remove extra spaces:
\s+— matches one or more whitespace characters; replace with a single space to collapse multiple spaces. - Extract text between delimiters: Use capture groups, e.g.,
"<(.*?)>"to capture content inside angle brackets. - Replace phone number formats: For example,
\(\d{3}\) \d{3}-\d{4}matches (123) 456-7890. - Mask email addresses partially:
([a-zA-Z0-9._%+-])[^@]*@— replace with$1***@to show only the first letter of the local part.
Remember that regex is case-sensitive by default. To make it case-insensitive, you can use the inline flag (?i) at the start of the pattern, like (?i)apple.
REGEXREPLACE vs. SUBSTITUTE and FIND/REPLACE
You might wonder when to use REGEXREPLACE over simpler tools. Here’s a quick comparison:
- SUBSTITUTE replaces exact text occurrences. Use it when you know the exact string and don’t need patterns. It’s faster and easier to read.
- Find and replace (Ctrl+H) is a manual, one-time operation. It also supports regex if you check the “Search using regular expressions” box in the find dialog, but it doesn’t update dynamically.
- REGEXREPLACE is a formula, so it recalculates when source data changes. It’s ideal for ongoing data cleaning or when you need to automate transformations.
For example, to replace all commas with semicolons, =SUBSTITUTE(A1, ",", ";") is simpler. But to replace commas that are followed by a space and a digit (like in a list of numbers), you’d need regex: =REGEXREPLACE(A1, ",(?=\s\d)", ";").
Common Errors and How to Fix Them
Several pitfalls trip up users new to regex in Sheets:
- #REGEXEXTRACT errors — Actually,
REGEXREPLACEdoesn’t throw that error; it returns the original text if no match is found. But if your pattern is malformed, you’ll get a “Formula parse error”. Check for unescaped backslashes or unbalanced parentheses. - Backslash escaping — In Google Sheets formulas, to use a regex escape like
\d, you must write\\din the formula bar? No, that’s a common confusion. In the formula, you type"\d"— the backslash is not doubled in the formula string; it’s only doubled when you’re writing the regex pattern in a programming language. In Sheets, you simply write"\d"(single backslash in the formula). The confusion arises because when you see the formula in the cell, it shows\d? Actually, the formula bar displays the backslash as typed. So type=REGEXREPLACE(A1, "\d", "")to remove digits. - Replacement text with special characters — If your replacement contains a dollar sign or backslash, you may need to escape it. For example, to replace with a literal dollar sign, use
"\$"? Actually, in the replacement, to insert a literal$, you need to escape it as\$? No, in Sheets replacement,$1is interpreted as a capture group. To insert a literal dollar sign, you need to use$$? According to RE2, to include a literal$in the replacement, you write$$. For a literal backslash, write\\. - Pattern not matching — Test your regex with a tool like regex101.com (using RE2 flavor) before applying it to your data. Also, ensure you haven’t accidentally included leading/trailing spaces in the pattern.
Frequently Asked Questions
Can I use REGEXREPLACE to remove all spaces from a cell? Yes: =REGEXREPLACE(A1, "\s", "") removes all whitespace characters (spaces, tabs, newlines). If you only want to remove spaces (not tabs), use " " (a space) as the pattern.
How do I replace multiple different patterns at once? You can nest REGEXREPLACE functions: =REGEXREPLACE(REGEXREPLACE(A1, "pattern1", "replacement1"), "pattern2", "replacement2"). Or use the alternation operator | in a single pattern, like "cat|dog" to match either word.
Is REGEXREPLACE case-insensitive by default? No, it’s case-sensitive. To make it case-insensitive, add (?i) at the start of the pattern, e.g., "(?i)apple" matches “Apple”, “APPLE”, etc.
What does the #N/A error mean when using REGEXREPLACE? REGEXREPLACE doesn’t return #N/A; it returns the original text if no match. If you see #N/A, it’s likely from a different function in the formula. For REGEXEXTRACT, #N/A means no match was found.
Can I use REGEXREPLACE in conditional formatting or data validation? Not directly. Conditional formatting uses custom formulas that can include REGEXMATCH but not REGEXREPLACE (since it doesn’t return a boolean). For data validation, you can use REGEXMATCH in a custom formula, but not REGEXREPLACE.
Performance and Limitations
Google Sheets has a cell calculation limit of 50,000 characters for the text argument in REGEXREPLACE. If your text exceeds that, the function returns an error. For very large datasets, using REGEXREPLACE in an array formula (with ARRAYFORMULA) can be slower than using Find & Replace, but it’s still manageable for typical spreadsheet sizes. Also, note that regex patterns are evaluated per cell, so if you have thousands of rows, it may take a moment to recalculate.
Alternatives and Related Functions
If REGEXREPLACE feels too complex, consider these alternatives:
- SUBSTITUTE for simple literal replacements.
- SPLIT and JOIN to break text into parts and reassemble.
- TRIM to remove extra spaces at the start/end.
- CLEAN to remove non-printable characters.
- REGEXEXTRACT to pull out matching parts, which you can then use in other formulas.
For example, to extract a domain from an email, use =REGEXEXTRACT(A1, "@([^@]+)$") and then you could use REGEXREPLACE to clean it further.
In summary, REGEXREPLACE is a powerful tool for pattern-based text manipulation in Google Sheets. Start with simple patterns, test them on a small sample, and gradually incorporate capture groups and flags to handle complex cleaning tasks. With practice, you’ll find it indispensable for transforming messy data into a clean, usable format.
Related
- Forecast Worksheet Excel: How to Build One
- Public Data Entry: What It Is & How It Works
- Generating a Random Number in Java
Try it: the Date Difference Calculator: days, months, years and weekdays between two dates, with the DATEDIF formulas.
More in How-to basics
- Google Sheets Freeze Rows Shortcut and Menu StepsGoogle Sheets has no dedicated keyboard shortcut that immediately freezes rows. In a web browser, use View >…
- Snowflake SPLIT Syntax: Arrays, Examples, and TipsIn Snowflake, use SPLIT(string, separator) to divide a string wherever a literal separator occurs; the function returns an…
- Excel Save Custom Number Formats: A Practical GuideExcel saves a custom number format inside the workbook: select the cells, press Ctrl+1, choose Number > Custom,…
- Excel Lowercase: Convert Text to LowercaseTo convert text to lowercase in Excel, use the LOWER function: =LOWER(A1) returns the text in cell A1…
- The Z Table in Statistics: How to Read and Use ItA z table in statistics is a reference chart that gives probabilities for values in the standard normal…
- Excel Shortcut Keys Interview Questions and AnswersExcel shortcut-key interview questions usually test whether you can move around a worksheet, edit and format data, and…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.