Excel’s new REGEXTEST, REGEXEXTRACT, and REGEXREPLACE functions let you find, pull out, and replace text using regular expressions—patterns that match specific character sequences. This guide walks you through each function, shows you how to write patterns, and covers common pitfalls so you can start cleaning and parsing data faster.
What You’ll Need
- Microsoft 365 subscription (these functions are rolling out to Current Channel).
- Excel for Windows, Mac, or the web—not available in Excel 2019 or earlier.
- Basic familiarity with Excel formulas (e.g., how to enter a formula and copy it down).
Step-by-Step
- Open a worksheet with text data. For example, column A might contain product codes like “AB-1234-XY” or full names like “John Smith”.
- Understand the three functions:
REGEXTEST(text, pattern, [case_sensitivity])— returns TRUE if the pattern matches anywhere in the text, FALSE otherwise.REGEXEXTRACT(text, pattern, [return_mode], [case_sensitivity])— returns the first match, all matches (as a vertical array), or capture groups.REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])— replaces matched text with something else.
- Write a simple pattern. Patterns are enclosed in double quotes. For example, to find any 4-digit number, use
"\d{4}". To find a literal dot, escape it:"\.". - Use REGEXTEST to check if a pattern exists. In cell B2, enter:
=REGEXTEST(A2, "\d{3}-\d{4}"). This returns TRUE if the text contains a 3-digit number, a hyphen, and a 4-digit number (like a phone number). Drag down to test multiple cells. - Extract text with REGEXEXTRACT. In cell C2, enter:
=REGEXEXTRACT(A2, "\d{4}")to get the first 4-digit number. To get all matches, use=REGEXEXTRACT(A2, "\d{4}", 1)—this spills results down. To capture specific parts, use parentheses:=REGEXEXTRACT(A2, "(\d{3})-(\d{4})", 2)returns the second capture group. - Replace text with REGEXREPLACE. In cell D2, enter:
=REGEXREPLACE(A2, "\d{4}", "XXXX")to mask all 4-digit numbers. To replace only the first occurrence, add a 4th argument:=REGEXREPLACE(A2, "\d{4}", "XXXX", 1). - Control case sensitivity. The last argument in each function is 0 for case-sensitive (default) or 1 for case-insensitive. For example,
=REGEXTEST(A2, "abc", 1)matches “ABC”, “Abc”, etc. - Test and refine your pattern. Start with a simple pattern and check results. Use online regex testers to debug, but remember Excel uses RE2 syntax—not all features (like lookaheads) are supported.
Common Mistakes
- Forgetting to escape special characters. A period matches any character, so to match a literal dot you need
\., not.. - Using unsupported regex features. Excel’s regex engine does not support lookaheads, lookbehinds, or backreferences. Stick to basic character classes, quantifiers, and groups.
- Confusing REGEXEXTRACT’s return modes. The second argument (return_mode) is 0 for first match, 1 for all matches, 2 for capture groups. Many users accidentally use 1 when they want the first match.
- Assuming REGEXTEST is case-insensitive by default. It is case-sensitive unless you set the third argument to 1.
- Not accounting for spaces or line breaks. Patterns like
\smatch whitespace, but literal spaces in patterns must match exactly.
Tips
- Use
\dfor digits,\wfor letters/digits/underscore, and\sfor whitespace—these shortcuts save time. - Test patterns on a small sample first. Copy a few values to a scratch area and run the formula there.
- Combine REGEXEXTRACT with other functions like
VALUE()to convert extracted text to numbers:=VALUE(REGEXEXTRACT(A2, "\d+")). - For complex extractions, use capture groups (parentheses) and return_mode 2 to pull specific parts.
- If you need to replace across multiple cells, use the fill handle or double-click to copy the formula down.
- Remember that REGEXEXTRACT with return_mode 1 returns a dynamic array—make sure you have space for the results to spill.
Troubleshooting
- #NAME? error — Your Excel version doesn’t support these functions. Check your Microsoft 365 update channel or use legacy methods (e.g., FIND, MID, SUBSTITUTE) as a workaround.
- #VALUE! error — The pattern is invalid or the text is not a string. Check for unescaped special characters or ensure the cell contains text, not a number.
- #SPILL! error — REGEXEXTRACT with return_mode 1 is trying to spill results but something is blocking the cells below. Clear the range or move the formula to a column with empty cells.
- No matches returned — Your pattern doesn’t match the text. Double-check the pattern against a regex tester, and verify case sensitivity.
- Unexpected matches — A character like
.matches any character. Use\.for a literal dot, and escape other special characters as needed. - Function not appearing in formula autocomplete — Ensure you’re on the latest version. If not, type the full function name manually—it may still work.
Frequently Asked Questions
Can I use regex in older versions of Excel?
No, these functions require Microsoft 365. For older versions, you can use VBA (User Defined Functions) or complex formulas with FIND, MID, and SUBSTITUTE, but they are less flexible.
How do I extract all numbers from a cell?
Use =REGEXEXTRACT(A1, "\d+", 1) — this returns all sequences of digits as a vertical array. If you want them in a single cell separated by commas, you’d need to combine with TEXTJOIN, but that’s not directly supported.
Is regex case-sensitive by default?
Yes, all three functions are case-sensitive unless you set the last argument to 1 (case-insensitive). For example, =REGEXTEST("ABC", "abc", 1) returns TRUE.
Can I use regex to replace text in multiple cells at once?
Yes, enter the REGEXREPLACE formula in one cell and drag it down, or use it in a table column. It works like any other formula.
What regex syntax does Excel support?
Excel uses RE2 syntax, which supports character classes, quantifiers, alternation, groups, and anchors. It does not support lookaheads, lookbehinds, or backreferences.
How do I match a literal dollar sign or backslash?
Escape it with a backslash: \$ for a dollar sign, \\ for a backslash. Remember that in Excel formulas, you need double backslashes in the pattern string.
Related
General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.
More in this bed
- Facebook Keeps Crashing on iOS 16? Fixes That Work
- Send Apple Pay in a Group Chat: iPhone & iPad
- Fix Cortana Not Working in Windows 11
- Fix iPhone Speaker Crackling: Step-by-Step
- Antimicrobial USB Drives: Claims and Limits
- How to Enable Developer Mode on Discord