REGEXEXTRACT pulls out the part of text that matches a regular expression. Where TEXTBEFORE and MID need a fixed delimiter or position, REGEXEXTRACT finds text by its shape: the first run of digits, an invoice number, a postcode, a date in a sentence.
Availability
REGEXEXTRACT belongs to the three REGEX functions in Microsoft 365, with REGEXTEST and REGEXREPLACE. They have been released in stages, so your build may not have them yet. There the formula returns #NAME?. They are not in Excel 2024 or earlier. The practice sheet here uses Rangewise's own engine, which does not run them yet, so the examples show what Excel returns and the exercise uses the older functions.
The arguments
- text is the text to search.
- pattern is the regular expression.
- return_mode chooses what comes back.
0 returns the first match. 1 returns all matches. 2 returns the capture groups of the first match, which are the bracketed parts of the pattern. - case_sensitivity is case-sensitive by default.
1 ignores case.
Examples of patterns
=REGEXEXTRACT("Please pay INV-2041 by Friday","INV-\d+") returns INV-2041. The pattern means "INV- followed by one or more digits", so it works for INV-9 and INV-20418 alike.
=REGEXEXTRACT("Order 7719 shipped","\d+") returns 7719, the first run of digits, wherever it sits in the text.
The result is text
Even when the match is digits, REGEXEXTRACT returns text. "7719" is not the number 7719. Wrap the formula in VALUE to get a number: =VALUE(REGEXEXTRACT(A2,"\d+")).
When nothing matches
If the pattern is not found, REGEXEXTRACT returns #N/A. That is useful as a signal, but it can break a report. Wrap it in IFERROR to show a blank or a message: =IFERROR(REGEXEXTRACT(A2,"\d+"),"none").
All matches and groups
With return_mode 1, the formula returns every match as an array, which spills into several cells. That is the one to use for "list every number in this text". With return_mode 2, brackets in the pattern mark capture groups, and each group becomes one element of the result. A pattern such as (\d{4})-(\d{2})-(\d{2}) splits a date into year, month and day in one step. Keep the spill range empty, or you get #SPILL!.
Before REGEX
Without REGEXEXTRACT, extraction needs combinations of SEARCH, MID, TEXTBEFORE and TEXTAFTER. They work well when the text has a reliable delimiter, such as the INV- prefix and a following space. They struggle when the shape varies, which is where a pattern is the better tool. They also work in versions of Excel that do not have the REGEX functions.