Skip to content

REGEXEXTRACT

Extracts the part of text that matches a regular expression: the first match, every match, or the capture groups of the first match.

Syntax

=REGEXEXTRACT(, , , )

TextStatic examples

Excel for Microsoft 365 only, and only on builds that include it. Microsoft has rolled out the REGEX functions in stages by update channel, so on a build without them the formula returns #NAME?. Not available in Excel 2024 or earlier. The practice sheet on this page cannot run REGEXEXTRACT yet.

How REGEXEXTRACT works

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.

Arguments

text
The text to search. Usually a cell such as A2.
pattern
The regular expression that describes what to extract, in quotes, such as "INV-\d+".
return_modeoptional
0, the default, returns the first match. 1 returns all matches as an array that spills. 2 returns the capture groups, the parts of the pattern in brackets, of the first match as an array.
case_sensitivityoptional
0, the default, is case-sensitive. 1 ignores case.

Examples

Example 1

Extract an invoice reference from a sentence

C2 =REGEXEXTRACT(A2,"INV-\d+")

Result: INV-2041

The pattern is INV- followed by one or more digits. This is what Excel returns; the practice sheet here cannot run the function yet.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 1: Extract an invoice reference from a sentence
RowABC
1Email subjectSenderReference
2Please pay INV-2041 by FridaySupplierINV-2041
3
4
5

Example 2

First run of digits in text

C2 =REGEXEXTRACT(A2,"\d+")

Result: 7719

The result is the text "7719", not a number.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 2: First run of digits in text
RowABC
1Status lineSourceOrder number
2Order 7719 shippedWarehouse7719
3
4
5

Example 3

Convert the match into a number

C2 =VALUE(REGEXEXTRACT(A2,"\d+"))

Result: 7719

VALUE turns the extracted text into a number you can calculate with.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 3: Convert the match into a number
RowABC
1Status lineSourceOrder number
2Order 7719 shippedWarehouse7719
3
4
5

Example 4

No match returns #N/A

C2 =REGEXEXTRACT(A2,"\d+")

Result: #N/A

There is no digit in the text, so there is nothing to return. IFERROR can replace the error with a message.

Shown as a static example: our practice sheet doesn't calculate this function yet.

Example 4: No match returns #N/A
RowABC
1Status lineSourceOrder number
2Awaiting stockWarehouse#N/A
3
4
5

Your turn.

Solve it in the sheet. Every answer is checked, with hints if you need them.

REGEXEXTRACT can't run in this sheet yet, so extract the reference the older way. In B2, return the invoice reference, such as INV-2041, from the subject in A2. Every reference is 8 characters long. Fill it down to B5.

=MID(A2,SEARCH("INV-",A2),8)

In C2, return just the four digits of the reference in A2 as a number, for example 2041. Fill it down to C5.

=VALUE(MID(A2,SEARCH("INV-",A2)+4,4))

Common mistakes

  • REGEXEXTRACT isn't in every version of Excel. Where it is missing the formula returns #NAME?. Check which version people will use first.
  • The result is always text. A matched number won't add up until you wrap the formula in VALUE.
  • No match returns #N/A. Wrap the formula in IFERROR when some rows have nothing to extract.
  • Matching is case-sensitive by default. Set the fourth argument to 1 to ignore case.
  • A pattern such as \d+ returns only the first run of digits. To get all of them, use return_mode 1, and leave room for the spill.
  • Patterns are not wildcards. . means any one character and * repeats the previous item, so patterns written for COUNTIF or SEARCH need rewriting.

Questions people ask

How do I extract numbers from text in Excel?

In Microsoft 365 builds that have it, `=REGEXEXTRACT(A2,"\d+")` returns the first run of digits. Wrap it in VALUE for a number. In other versions, combine SEARCH, MID or TEXTAFTER with a known marker.

Why does REGEXEXTRACT return #N/A?

The pattern was not found in the text. Check the pattern and the case, or wrap the formula in IFERROR to show a message instead.

Why does REGEXEXTRACT return #NAME?

Your version of Excel doesn't have the function yet. It is Microsoft 365 only and has been released in stages. Use the older text functions or update your build.

How do I extract every match, not just the first?

Set return_mode to 1, as in `=REGEXEXTRACT(A2,"\d+",1)`. The matches spill into several cells, so keep the area they spill into empty.

What is the difference between REGEXEXTRACT and TEXTBEFORE or TEXTAFTER?

TEXTBEFORE and TEXTAFTER cut at a known delimiter. REGEXEXTRACT finds text by its pattern, so it works when the surrounding text varies.

Learn REGEXEXTRACT in a real sheet.

Tracks take you from the basics to modelling, one graded exercise at a time.