Regex in Excel: REGEXTEST, REGEXEXTRACT and REGEXREPLACE describe the appearance of the text and replace nested LEFT, MID and FIND.
FAQ — What does this article answer?
Q: What do these three functions actually do?
A: They split one job — working with text by pattern — into three. REGEXTEST checks whether text matches a pattern and returns TRUE or FALSE. REGEXEXTRACT pulls the matching part out. REGEXREPLACE transforms the matching part. All three describe what the text looks like rather than where in the string it sits.
Q: How does this differ from the LEFT, MID, FIND and SUBSTITUTE functions?
A: The classic functions work based on fixed positions, such as character 1 to 3 or everything after the first space. This approach breaks down when the data varies. A regular expression, however, matches by shape, 'four digits', 'an email address', 'anything that isn't a letter', so one formula can handle entries that the position-based approach would require a multitude of nested functions to cover.
Q: Which Excel versions have them?
A: REGEXTEST, REGEXEXTRACT and REGEXREPLACE are available only in Microsoft 365 (Windows and Mac) and Excel for the web. As they were released after the perpetual Excel 2024 version, so they are not included in Excel 2024, 2021, 2019 or 2016, where they return #NAME?.
Q: Do I need to learn about regular expressions?
A: Not much and certainly less than you might think. A dozen tokens cover most business tasks, and Part 2 is a one-minute cheat sheet. The examples here use only that core set.
Q: When should I stick with the classic text functions?
A: When the position is fixed and known, when a single FIND, MID or SUBSTITUTE already does the job, when the workbook must open in Excel 2024 or an earlier version; or when those maintaining it don't understand regular expressions. Part 8 draws the line.
In Excel, cleaning text has long involved creating the same kind of fragile machinery: a MID wrapped around two FIND's to extract a code from the middle of a string, a series of nested SUBSTITUTE's to remove unwanted characters, and an IF over a SEARCH to verify the validity of an entry. Each of these approaches works until the data varies, a different separator, an extra space or a missing prefix, for example, at which point they quietly return the wrong answer.
Three functions released to Microsoft 365 in 2024 change this approach: REGEXTEST, REGEXEXTRACT and REGEXREPLACE bring regular expressions, a compact, widely understood language for describing text patterns, directly into the formula bar. Rather than telling Excel where something is, you describe what it looks like, and the same pattern language then tests, extracts, or transforms. This article is as much about this shift in approach as it is about the three functions, and importantly, it is about knowing when a pattern is the right tool for the job and when a classic text function is still the better option.
Part 1: Three functions, one idea for testing, extracting and replacing
The three functions all share the same basic concept: a pattern that describes the shape of the text. The difference lies in what each function does once it finds a match.
REGEXTEST asks a simple yes/no question: does the text match? It is the tool for validating an entry or flagging rows that contain a pattern. REGEXEXTRACT extracts the matching substring, enabling you to pull an invoice number, code or ID out of free text. REGEXREPLACE replaces the matching substring with something else. This allows you to clean, reformat and reorder in a single step. Each one replaces a familiar series of classic functions, such as an IF/SEARCH test, a MID/FIND extraction or a series of SUBSTITUTEs, with a single expression driven by one pattern.
Part 2: A one-minute regex primer
A regular expression is mostly ordinary text, where letters and digits match themselves, with a small set of metacharacters that represent categories or positions rather than literal characters.
The tokens worth knowing on day one:
| Token | Matches |
|---|---|
\d |
a digit, 0–9 (\w a letter, digit or underscore; \s a space or tab) |
. |
any single character |
[A-Z] |
a character in a range; [^…] any character except the ones listed |
^ … $ |
the start and end of the text (anchors) |
* + ? |
the preceding item repeated 0-or-more, 1-or-more, or 0-or-1 times |
{n} {n,m} |
exactly n times, or between n and m times |
( … ) |
a capturing group, reused in a replacement as $1, $2 |
a\|b |
a or b (alternation); \. a literal dot (escape a metacharacter with \) |
There are three points about Excel's implementation that are worth stating once. First, the engine is PCRE2, a widely used Perl-compatible flavour, so patterns you find in general expression references will usually work. Matching is case-sensitive by default, pass 1 as the final case_sensitivity argument to ignore case. In replacements, Excel uses $1 and $2 for captured groups, rather than the \1 and \2 used by many other tools use, This can be a real trap for anyone who already knows regular expressions.
💡 Tip: A small correctness note that catches people out is that the range [A-z] is not the same as [A-Za-z]. It also matches several punctuation characters that sit between Z and a in the character table. Use [A-Za-z] when you mean letters.
Part 3: The data we're working with
The examples below use a small sample of the messy text that these functions were designed for, such as supplier lines carrying invoice references, a column of email addresses of varying quality and customer IDs with inconsistent separators. Each example is presented in an Excel table so that the formulas can spill over and remain readable. The case for this approach is set out in Why every dataset should be an Excel table.
Part 4: REGEXTEST validates entries and finds patterns
Of the three, REGEXTEST is the simplest of the three: it returns TRUE or FALSE depending on whether the text matches.
=REGEXTEST(text, pattern, [case_sensitivity])
To validate an email address:
=REGEXTEST(A2, "^[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,}$", 1)
One point matters more than the pattern itself here. REGEXTEST returns TRUE if any part of the text matches, so to validate a whole cell, the pattern must be anchored with ^ at the start and $ at the end. Without the anchors, "bad email@acme.com" would be accepted as valid, because it contains a valid-looking email address. Anchoring distinguishes between asking "is this a valid email?" and "does this contain something email-shaped?".
The second question, which is unanchored, is useful in its own right. It is a way of finding a pattern, rather than validating one. To flag every row that contains an invoice reference:
=REGEXTEST(A2, "INV-\d{4}-\d{5}")
As REGEXTEST accepts a range and spills, it naturally pairs with FILTER to return only the matching rows: =FILTER(data, REGEXTEST(data, "…")). This builds on the dynamic array behaviour covered in Dynamic arrays with FILTER, SORT, UNIQUE and SEQUENCE. For pattern-based data checks, it also complements the list-based approach in Data validation with dropdowns and dependent lists.
Part 5: Using REGEXEXTRACT to pull invoice numbers and codes out of text
While REGEXTEST answers with a simple yes or no, REGEXEXTRACT returns the matching text itself.
=REGEXEXTRACT(text, pattern, [return_mode], [case_sensitivity])
To lift the invoice reference out of a supplier line, regardless of what surrounds it:
=REGEXEXTRACT(A2, "INV-\d{4}-\d{5}")
The return_mode argument determines the output: 0 returns the first match (the default), 1 returns all matches as a spilled array, and 2 returns the capturing groups of the first match. The last mode is used to extract just part of a match. To pull only the five-digit sequence, wrap it in a group and ask for the group:
=REGEXEXTRACT(A2, "INV-\d{4}-(\d{5})", 2) ' returns 00841
If no match is found, REGEXEXTRACT returns a #N/A error rather than an empty string. Wrap it in IFERROR to present a clean fallback — =IFERROR(REGEXEXTRACT(A2, "…"), "—") — using the error-handling patterns from Handling errors with IFERROR and IFNA.
Part 6: REGEXREPLACE cleans IDs and reformats text
REGEXREPLACE searches for a pattern and replace it with something else, it's the ideal tool for cleaning and reformatting text.
=REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity])
To normalise customer IDs that contain stray spaces, dashes or underscores, replace any non-letter or non-digit characters with nothing:
=REGEXREPLACE(A2, "[^A-Za-z0-9]", "") ' "CUST 00123" → "CUST00123"
The same process strips a string down to digits only with =REGEXREPLACE(A2, "\D", ""). The occurrence argument controls which match is replaced: 0 (the default) replaces every match, a positive number replaces only that occurrence, and a negative number counts from the end.
The most powerful technique is to reuse captured groups in the replacement. To reorder a name from "First Last" to "Last, First", capture each word and refer to them as $2 and $1:
=REGEXREPLACE("James Henry", "(\w+) (\w+)", "$2, $1") ' returns "Henry, James"
This technique can also be used to reformat structured text, for example, converting (378) 555-4195 into 378-555-4195 by capturing the three number groups and rejoining them with dashes. Note the Excel-specific detail that the replacement uses $1 and $2, not \1 and \2.
Part 7: Putting it together with the wider toolkit
These functions are most useful when used alongside the other modern tools in the toolkit. REGEXTEST inside FILTER returns every row that matches a pattern. REGEXEXTRACT with the return mode set to 1 spills all matches, ready to be fed into UNIQUE or SORT. Furthermore, since December 2024, the three functions have been joined by XLOOKUP and XMATCH, which now accept a regular expression as the lookup value when their match mode is set to 3. This means that a single lookup can match several spellings at once, as demonstrated by the following example: =XLOOKUP("USA|United States|US", A:A, B:B, "Not found", 3). This connection to the lookup family is covered in XLOOKUP, INDEX and MATCH.
Part 8: Choosing the right technology
Regular expressions are powerful, but they are not the answer to every text-processing task. Using them effectively means recognising when a classic text function is a better solution.
Use the regex functions when the pattern varies, for example, any digit, any group of letters, or an address of unpredictable length, when one formula must validate, extract or reformat in a single step, when the classic equivalent would require several nested functions, and when your workbook runs on Microsoft 365 or the web.
Use the classic text functions, LEFT, RIGHT, MID, FIND, SEARCH, SUBSTITUTE, TEXTBEFORE, TEXTAFTER, TEXTSPLIT, when the position is fixed and known (the first three characters, everything after the first space), when a single FIND or MID already does the job, and when the people maintaining the workbook don't read RegEx. Their mechanics are set out in Parse and combine text with formulas and TEXTSPLIT, TEXTBEFORE and TEXTAFTER.
Availability is what makes this decision concrete. The regular expression (regex) functions only run in Microsoft 365 and Excel for the web. They are absent from Excel 2021 and earlier versions, where they return the error code #NAME?. Therefore, if a workbook must open in an older version, the classic text functions are not just an alternative option, they are the fallback that will actually run. The full picture can be found in Excel 365 versus older versions. For bulk cleaning across many files rather than a live in-sheet formula, Power Query remains the more powerful option, as discussed in How to clean messy data in Excel.
Part 9: Common failure modes
| Symptom | Likely cause | Fix |
|---|---|---|
#NAME? or _xlfn.REGEXTEST |
Opened in Excel 2024 or earlier | Use classic text functions or Power Query; see Part 8 |
#N/A from REGEXEXTRACT |
No part of the text matched the pattern | Wrap in IFERROR, or loosen the pattern |
| A partial match passes validation | The pattern isn't anchored | Add ^ at the start and $ at the end |
Replacement inserts $1 literally |
Used \1 instead of $1 for the group |
Use $1, $2 in the replacement text |
| Case doesn't match as expected | Matching is case-sensitive by default | Pass 1 as the case_sensitivity argument |
| A pattern grabs too much | A greedy quantifier (.*, .+) ran on |
Make it lazy (.*?) or tighten the character class |
[A-z] matches odd characters |
The range spans punctuation between Z and a |
Write [A-Za-z] |
#VALUE! |
An invalid pattern, or a non-text argument | Check the pattern's syntax and the input's type |
Summary
The real value of these three functions lies in their ability to transform position-based text processing into pattern-based description.
REGEXTESTreturnsTRUEorFALSEto validate an entry (anchored with^…$) or to find rows containing a pattern (unanchored).REGEXEXTRACTextracts the matching text out; thereturn_modechooses the first match, all matches, or the capturing groups.REGEXREPLACEtransforms the match by stripping characters, reformatting, or reordering using captured groups as$1,$2, etc.- One pattern can replace a stack of nested
LEFT,MID,FINDandSUBSTITUTEfunctions. - Reach for RegEx when the pattern varies and one formula should do the work and stay with classic text functions when positions are fixed and a single function suffices, or the workbook must open in an older version.
- They only run in Microsoft 365 and Excel for the web, not in Excel 2024 or earlier versions, where they return
#NAME?.
The principle is the same as before: describe the problem in the clearest way possible using the tool, and be deliberate about when an older version constraint or simpler task means that a classic text function is the better answer.
See also: Parse and combine text with formulas for the classic text-function approach these patterns replace, TEXTSPLIT, TEXTBEFORE and TEXTAFTER for the modern position-based text functions, How to clean messy data in Excel for the wider cleaning toolkit, Data validation with dropdowns and dependent lists for controlling entries at the point of input, Handling errors with IFERROR and IFNA for tidying a #N/A from a missed match, and Excel 365 versus older versions for the full availability picture.
This article is part of the helpme.safeoffice.de series, which provides practical guides on Excel functions, workbook modeling and data solutions. The series is aimed at businesses that want effective, maintainable tools that everyone can understand.