Excel REGEX Functions: REGEXTEST, REGEXEXTRACT, REGEXREPLACE
By ExcelReady TeamUpdated 6 min read
The short answer
Excel has three regex functions in Microsoft 365: REGEXTEST checks whether text matches a pattern and returns TRUE or FALSE, REGEXEXTRACT pulls out the matching text, and REGEXREPLACE swaps it for something else. For example, =REGEXEXTRACT(A2,"\d+") returns the first number in A2. Older versions show #NAME? and need FIND, SEARCH or SUBSTITUTE.
Key takeaways
- REGEXTEST(text, pattern) returns TRUE or FALSE. REGEXEXTRACT returns the match. REGEXREPLACE replaces it.
- \d means a digit, \s a space, \w a letter, digit or underscore, and + means one or more.
- Matching is case-sensitive by default. Set the case_sensitivity argument to 1 to ignore case.
- REGEXEXTRACT returns text. Wrap it in VALUE or -- if you need a number.
- If you see #NAME?, your Excel doesn't have them yet. Use FIND, SEARCH, MID and SUBSTITUTE instead.
On this page+
Regex in Excel means three functions: REGEXTEST, REGEXEXTRACT and REGEXREPLACE. A regular expression (regex) is a short pattern that describes text, such as "four digits" or "anything before the @". With these functions you can check, extract and clean text that would need long FIND and MID formulas before. This guide explains the syntax, the pattern symbols you'll use most, and what to do if your Excel doesn't have them.
What are the REGEX functions in Excel?#
Excel's regex functions each take a piece of text and a pattern. They differ in what they give back.
| Function | Syntax | Returns |
|---|---|---|
| REGEXTEST | =REGEXTEST(text, pattern, [case_sensitivity]) | TRUE if the pattern is found, otherwise FALSE |
| REGEXEXTRACT | =REGEXEXTRACT(text, pattern, [return_mode], [case_sensitivity]) | The matching text |
| REGEXREPLACE | =REGEXREPLACE(text, pattern, replacement, [occurrence], [case_sensitivity]) | The text with matches replaced |
Version note: Microsoft added these functions to Microsoft 365 in 2024, first for Insiders and then for everyone on current builds. They work in Excel for Microsoft 365 on Windows and Mac, and in Excel for the web. Excel 2019 and Excel 2021 don't have them. If you use Excel 2024 or another one-time-purchase version, type =REGEXTEST( in a cell to check: if Excel doesn't suggest it, or the formula shows #NAME?, your version doesn't support it.
The patterns follow a common regex style called PCRE2, so most patterns you find in regex tutorials work here too.
Regex pattern basics#
A pattern mixes plain characters, which match themselves, with special symbols. You only need a handful to start:
| Symbol | Means | Example pattern | Matches |
|---|---|---|---|
\d | Any digit 0–9 | \d\d | 42 |
\D | Any character that is not a digit | \D | the dash in 555-0142 |
\w | A letter, digit or underscore | \w+ | Anna |
\s | A space, tab or line break | \s+ | one or more spaces |
. | Any single character | A.C | ABC, A-C |
+ | One or more of the previous item | \d+ | 7, 4821 |
* | Zero or more of the previous item | ab* | a, ab, abbb |
{n} | Exactly n of the previous item | \d{4} | 2024 |
[ ] | Any one character in the set | [A-Z] | any capital letter |
^ and $ | Start and end of the text | ^INV | text starting with INV |
( ) | A capture group | (\d+)-(\d+) | 555-0142, as two parts |
\. | A real full stop | \.com | .com |
Patterns go in double quotes like any text in a formula. If a pattern needs a double quote, type it twice: "".
REGEXTEST: check if text matches a pattern#
REGEXTEST returns TRUE when the pattern appears in the text. Use it to check codes, IDs and formats. Say A2 holds INV-2024-0583:
=REGEXTEST(A2,"^INV-\d{4}-\d{4}$")The result is TRUE. The pattern means: start of the text, INV-, four digits, a dash, four digits, end of the text. INV-24-583 would return FALSE. Without ^ and $, the pattern could match inside longer text.
Matching is case-sensitive by default, so inv-2024-0583 fails too. Add 1 as the third argument to ignore case: =REGEXTEST(A2,"^inv-\d{4}-\d{4}$",1). Combine REGEXTEST with IF or FILTER to flag or list the rows that pass.
REGEXEXTRACT: pull text out of a cell#
REGEXEXTRACT returns the part of the text that matches. If A3 holds Order 4821, 3 items:
| Formula | Result |
|---|---|
=REGEXEXTRACT(A3,"\d+") | 4821 (the first match) |
=REGEXEXTRACT(A3,"\d+",1) | 4821 and 3, spilled into two cells |
=VALUE(REGEXEXTRACT(A3,"\d+")) | 4821 as a real number |
The third argument, return_mode, picks what comes back: 0 (the default) is the first match, 1 is every match, and 2 is the capture groups of the first match. For a date written as 2024-11-03, =REGEXEXTRACT(A4,"(\d{4})-(\d{2})-(\d{2})",2) returns 2024, 11 and 03 in separate cells.
The result is always text, so a number on the left of the cell won't add up. Wrap it in VALUE or put -- in front. For the non-regex ways to do this, see how to extract numbers from text in Excel.
A practical example: to pull an email address out of a sentence like Contact: anna.lee@example.com, use =REGEXEXTRACT(A5,"[\w.+-]+@[\w-]+\.[\w.]+"). It returns anna.lee@example.com. Real email rules are more complex, so treat this as a quick extractor, not a full validator.
REGEXREPLACE: clean and reformat text#
REGEXREPLACE finds every match and replaces it. It is the regex version of SUBSTITUTE, and it can replace patterns, not just fixed text.
| Text in A2 | Formula | Result |
|---|---|---|
Call 555-0142 now | =REGEXREPLACE(A2,"\D","") | 5550142 |
Lee, Anna | =REGEXREPLACE(A2,"\s+"," ") | Lee, Anna |
Lee, Anna | =REGEXREPLACE(A2,"(\w+), (\w+)","$2 $1") | Anna Lee |
The first formula deletes every non-digit. The second turns any run of spaces into one space. The third uses capture groups: $1 is the first group in brackets and $2 is the second, so the name is flipped.
The optional occurrence argument chooses which match to replace. Leave it out, or use 0, to replace all of them. Use 1 for the first match only.
No REGEX functions? Use FIND, SEARCH and SUBSTITUTE#
Every version of Excel can do the common regex jobs with older text functions. The formulas are longer, but they are what most job tests expect.
| Task | Regex formula | Formula for any version |
|---|---|---|
| Number after a # | =REGEXEXTRACT(A2,"\d+") | =MID(A2,SEARCH("#",A2)+1,4) |
| Remove dashes | =REGEXREPLACE(A2,"-","") | =SUBSTITUTE(A2,"-","") |
| Starts with INV | =REGEXTEST(A2,"^INV") | =LEFT(A2,3)="INV" |
| Contains "late" | =REGEXTEST(A2,"late",1) | =ISNUMBER(SEARCH("late",A2)) |
SEARCH finds the position of text and ignores case, while FIND is case-sensitive. The SEARCH function guide and the article on FIND vs SEARCH for locating text show how to combine them with MID. SUBSTITUTE swaps fixed text: see the SUBSTITUTE function guide and SUBSTITUTE vs REPLACE. To strip unwanted characters, read how to remove characters in Excel.
For counting and summing, wildcards are a mini pattern language that works everywhere: =COUNTIF(A2:A20,"INV-*") counts IDs starting with INV-. The guide to wildcards in COUNTIF and SUMIF covers *, ? and ~.
Common REGEX errors and fixes#
Most problems come from the version, the pattern or the result type.
- #NAME? means your Excel doesn't have the function. Use the older formulas above.
- An error when nothing matches. REGEXEXTRACT can't return a match that isn't there. Wrap it in IFERROR:
=IFERROR(REGEXEXTRACT(A2,"\d+"),""). - FALSE when you expect TRUE. Check capital letters, since matching is case-sensitive by default, and check that
^and$aren't too strict. - Numbers that won't add up. The result is text. Wrap it in VALUE.
- A full stop that matches everything.
.means any character. Write\.for a real full stop.
Practise it#
Regex is powerful, but the building blocks are the classic text skills that job tests check. Practise FIND and SEARCH in a live spreadsheet: you'll pull order numbers out of text, just like the exercise here. Then practise cleaning messy text with TRIM and SUBSTITUTE. Each formula is checked as soon as you press Enter.
Try it yourself
This is a live spreadsheet. Type the formula in the green cell and press Enter.
Without regex, pull the 4-digit order number out of A2. In B2, use SEARCH to find the # sign, then MID to return the 4 characters after it.
| A | B | C | D | |
|---|---|---|---|---|
| 1 | Order number | |||
| 2 | Order #4821 shipped | |||
| 3 | Order #5310 delayed | |||
| 4 | Order #6045 shipped | |||
| 5 | Order #7182 on hold | |||
| 6 | Order #8237 shipped | |||
| 7 | ||||
| 8 | ||||
| 9 |
Tip: SEARCH("#",A2) gives the position of #. The number starts one character later, and it is 4 characters long.
Practise this in a lesson
Frequently asked questions
Does Excel support regex?
Yes, in Microsoft 365. Microsoft added REGEXTEST, REGEXEXTRACT and REGEXREPLACE in 2024. Older versions such as Excel 2019 and 2021 don't have them and show #NAME?.
How do I extract numbers from text with regex in Excel?
Use =REGEXEXTRACT(A2,"\d+") for the first number, or =REGEXEXTRACT(A2,"\d+",1) to return every number as a spilled list. The result is text, so wrap it in VALUE to calculate with it.
Is REGEXTEST case-sensitive?
Yes, by default. Add 1 as the third argument to ignore case, for example =REGEXTEST(A2,"^inv",1), which matches INV, Inv and inv.
What can I use instead of regex in older Excel?
Use SEARCH or FIND to locate text, MID, LEFT and RIGHT to pull it out, SUBSTITUTE to replace it, and wildcards like * and ? in COUNTIF. They need longer formulas but work in every version.