Sub topic 8
Extracting the First Match with REGEXEXTRACT
Prerequisites and working model
This chapter assumes that you can enter Google Sheets formulas, reference cells and ranges, and use functions such as TRIM and IFERROR. You should also know that text values are strings written inside double quotes.
REGEXEXTRACT searches text with a regular expression, which is a pattern describing the text you want to find. It returns the first matching part of the input. The function has this form:
=REGEXEXTRACT(text, regular_expression)For example, if A2 contains Contact: [email protected], this formula returns the email address:
=REGEXEXTRACT(A2, "[\w.+-]+@[\w-]+\.[\w.]+")The result is text, even when the matching characters look like a number.
How the first match works
A regular expression can describe letters, digits, separators, and repetition. In the email pattern above:
| Pattern | Meaning |
|---|---|
| `[\w.+-]+` | One or more letters, digits, underscores, periods, plus signs, or hyphens |
| `@` | A literal at sign |
| `[\w-]+` | One or more letters, digits, underscores, or hyphens |
| `\.` | A literal period |
| `[\w.]+` | One or more letters, digits, underscores, or periods |
REGEXEXTRACT scans from the beginning of the cell toward the end. If the cell contains two matching email-like strings, it returns the first one.
=REGEXEXTRACT("Send reports to [email protected] or [email protected]", "[\w.+-]+@[\w-]+\.[\w.]+")This returns [email protected].
The function searches inside larger text. The entire cell does not need to match the pattern. To require a match for the entire cell, use the anchors ^ and $. The caret means “start of text,” and the dollar sign means “end of text.”
=REGEXEXTRACT(A2, "^[A-Z]{3}-\d{5}$")This matches a cell containing exactly an ID such as ORD-10492. It does not match Order ORD-10492 because that value contains extra text.
A small pattern vocabulary
Use character classes to specify which characters are allowed. A character class is a group inside square brackets.
[A-Z]matches one uppercase letter.[a-z]matches one lowercase letter.[0-9]matches one digit.\dmatches a digit.\wmatches a word character.+means one or more of the preceding item.*means zero or more.?means zero or one..normally matches any character. Write\.when you mean a literal period.
Google Sheets uses the RE2 regular expression syntax. RE2 does not support every feature found in other regex engines, such as lookahead and lookbehind. Start with direct patterns that describe the text you need.
Understanding the search flow
The formula reads the input, tests possible positions from left to right, and returns the first substring that satisfies the pattern.
Use the result in another formula
Because the result is text, convert digit-only matches before doing arithmetic. VALUE converts numeric text into a number.
=VALUE(REGEXEXTRACT(A2, "\d+"))If A2 contains Ticket 10492, the formula returns the number 10492, which can be added or compared numerically.
For a column with mixed content, decide whether a missing match should remain visible or become blank. The next chapter discusses capture groups; here, focus on protecting a simple extraction:
=IFERROR(REGEXEXTRACT(A2, "[A-Z]{3}-\d+"), "")This returns a blank string when A2 contains no matching ID instead of displaying #N/A.
Exercise
A2 contains Order ORD-10492 shipped. Write a formula that extracts ORD-10492.
Show answer
=REGEXEXTRACT(A2, "[A-Z]{3}-\d+")