REGEXEXTRACT
Pull text out of a string using a regular expression pattern. Great for grabbing phone numbers, codes, or anything with a predictable shape.
Spreadsheet editor
Spreadsheet editor
Syntax
=REGEXEXTRACT(text, pattern, [return_mode], [case_sensitivity])
Returns: Text Arguments
| Argument | Required | Description |
|---|---|---|
| text | Yes | The text you want to search inside. Type it in quotation marks or point to a cell that holds the text. |
| pattern | Yes | The regular expression that describes what you want to pull out. Excel uses the PCRE2 style of regex. |
| return_mode | No | What to return. 0 (the default) gives the first match. 1 returns every match as an array. 2 returns the capturing groups (the parts you wrap in parentheses) from the first match as an array. |
| case_sensitivity | No | Set to 0 (the default) to treat uppercase and lowercase as different, or 1 to ignore case while matching. |
About
Use it when a simple delimiter split is not enough. Functions like TEXTAFTER and TEXTBEFORE work well when you can point at one character, but REGEXEXTRACT shines when you need to match a pattern instead. With the return_mode argument you can get just the first match, every match as an array, or the capturing groups (the pieces you wrap in parentheses) from the first match. The case_sensitivity argument lets you decide whether uppercase and lowercase should count as the same.
REGEXEXTRACT works in Microsoft 365 and uses the PCRE2 flavor of regex, the same style you may know from many programming languages. It always returns text, so wrap the result in VALUE when you need a real number. On older versions of Excel you can fall back to combinations of MID, FIND, and SEARCH, though they take far more work to set up.
Examples
Handle rows with no match
Spreadsheet editor
Add up matched numbers
Spreadsheet editor
Watch out for
#N/A when nothing matches
If the pattern finds nothing in the text, REGEXEXTRACT returns a #N/A error, which can break the cells that depend on it.
→ Wrap the formula in IFERROR to return a blank or a friendly message, like =IFERROR(REGEXEXTRACT(A2, "\d+"), "").
The result is text, not a number
REGEXEXTRACT always returns text, so a matched number like 2045 will not add up or sort as a number.
→ Convert it with VALUE, like =VALUE(REGEXEXTRACT(A2, "\d+")), to get a real number back.
Backslashes need care in patterns
Regex shortcuts like \d and \w start with a backslash, and it is easy to forget them or type the wrong one, which makes the pattern miss.
→ Build the pattern slowly and test it on a few rows. Use \d for a digit, \w for a letter or digit, and \s for a space.