Text function

REGEXEXTRACT function

Pulls text that matches a regular expression pattern out of a string: the first match, all matches, or the capture groups.

Available in Microsoft 365 only (Windows, Mac and Excel for the web; rolled out from 2024)

Microsoft 365 onlyDifferent in Google Sheets

Syntax

REGEXEXTRACT(text, pattern, [return_mode], [case_sensitivity])
ArgumentWhat it means
textRequiredThe text or cell to search.
patternRequiredThe regular expression describing what to extract, e.g. "\d+" for a run of digits.
return_modeOptional0 (default) first match; 1 all matches as an array; 2 the capture groups of the first match as an array.
case_sensitivityOptional0 (default) case-sensitive; 1 ignore case.

Returns: The matching text, or an array of matches/groups that spills.

How REGEXEXTRACT works

REGEXEXTRACT finds text by shape rather than by position. Instead of counting characters with MID and FIND, you describe what you want - "a run of digits", "something that looks like an email", "a word starting with #" - and Excel returns it. That makes it ideal for messy free text such as order notes, bank descriptions or log lines where the useful part can be anywhere.

The pattern uses the PCRE2 regular-expression flavour. Quick pattern guide: \d a digit, \w a letter, digit or underscore, \s whitespace, . any character, [A-Z] one capital letter, [^0-9] anything except a digit, + one or more, * zero or more, {4} exactly four, ^ start of text, $ end of text, ( ) a capture group, | or. With return_mode 1 you get every match (wrap it in TEXTJOIN to keep them in one cell), and with 2 you get the parts captured by each pair of brackets, which is perfect for splitting a match into pieces.

The result is always text, even when it looks like a number; put -- in front or use VALUE to calculate with it. If nothing matches, the function returns #N/A.

Examples

1Order number from a note

B2fx=REGEXEXTRACT(A2,"\d+")
AB
1Note
2Order 4471 shipped on 12 May4471

Result: 4471

\d+ means one or more digits. The first run of digits in the note is 4471 (the 12 comes later and is ignored in mode 0).

2All hashtags in one cell

B2fx=TEXTJOIN(", ",TRUE,REGEXEXTRACT(A2,"#\w+",1))
AB
1Post
2Loving the #summer sale at #Contoso#summer, #Contoso

Result: #summer, #Contoso

Mode 1 returns every match: #summer and #Contoso. TEXTJOIN collects them into one cell separated by commas.

3Domain via a capture group

B2fx=INDEX(REGEXEXTRACT(A2,"(\w+)@([\w.]+)",2),2)
AB
1Contact line
2Contact: ben@fabrikam.comfabrikam.com

Result: fabrikam.com

Mode 2 returns the two bracketed groups, ben and fabrikam.com. INDEX picks the second one.

4Amount as a real number

B2fx=--REGEXEXTRACT(A2,"[\d.]+")
AB
1Bank text
2Total: 129.90 EUR129.9

Result: 129.9

The pattern takes the first run of digits and dots, 129.90. The double minus converts the text to the number 129.9.

⬇Practice workbookExamples with live formulas + practice sheets · .xlsx · 17 KB

Common errors and how to fix them

ErrorWhy it happensFix
#N/AThe pattern does not match anything in the text.Wrap in IFERROR or IFNA, or test first with REGEXTEST.
#VALUE!The pattern is not a valid regular expression (e.g. an unclosed bracket) or return_mode is not 0, 1 or 2.Check brackets and escape special characters such as ., ( and + with a backslash.
#NAME?Your version of Excel does not have the REGEX functions.Use Microsoft 365, or fall back to MID/FIND formulas.

Tips

  • Remember to escape characters that have a special meaning: to match a literal dot write \., a literal bracket \(.
  • Mode 1 can spill several cells; make sure there is room or wrap it in TEXTJOIN as shown.
  • Test patterns on a few sample rows first; a regex that is too loose (like .*) happily matches more than you intended.

Questions

Which regex syntax does Excel use?

PCRE2, the same family used by PHP, R and many editors, so most tutorials and online testers (set to PCRE) apply directly.

How do I get all matches instead of the first one?

Set return_mode to 1. The matches spill as an array; wrap in TEXTJOIN(", ",TRUE, ...) to put them in one cell.

Why can't I sum the numbers REGEXEXTRACT returns?

Its results are text. Convert them with --REGEXEXTRACT(...) or VALUE.

Google Sheets

Exists as REGEXEXTRACT(text, regular_expression) using RE2 syntax; it returns the first match (or its capture groups) and has no return_mode or case_sensitivity arguments.

REGEXEXTRACT in other languages

In a non-English version of Excel the function has a local name. Excel translates formulas automatically when a file is opened in another language.

LanguageFunction name
DutchREGEXEXTRAHEREN
Portuguese (Portugal)REGEX.EXTRAIR
RussianРЕГИЗВЛЕЧЬ
SpanishREGEXEXTRACCION

Recipes that use REGEXEXTRACT