Sub topic 7
Just enough regular expressions
A regular expression (regex) is a short pattern that describes the shape of some text. REGEXEXTRACT uses one to pick a piece out of a cell. This chapter covers the part of regex syntax you need for the rest of the tutorial. It does not try to cover everything.
This chapter assumes you can enter formulas, reference cells, and write text in double quotes. It also assumes you know what IFERROR and TRIM do. It does not teach REGEXEXTRACT itself. The next chapter does that. Here, the examples use REGEXEXTRACT only to show what a pattern matches.
What a pattern is made of
A pattern is a sequence of pieces. Each piece matches one character, or a repeated run of characters. The engine scans the text from left to right and returns the first place where the whole pattern fits.
Google Sheets uses the RE2 syntax. RE2 is fast and predictable, but it leaves out a few features you may know from other languages. See the limits section below.
The simplest pattern is plain text. Letters, digits, and spaces match themselves.
=REGEXEXTRACT("Order ORD-10492 shipped", "ORD-10492")This returns ORD-10492. A fixed pattern like this is rarely useful, because you could find the text without a regex. The value of regex comes from pieces that match a category of characters or a variable amount of text.
Character classes
A character class matches one character out of a set. Put the set in square brackets.
[abc]matchesa,b, orc.[A-Z]matches any uppercase letter. A hyphen between two characters makes a range.[0-9]matches any digit.[^,]matches any character except a comma. The^at the start of the brackets means "not".
Common classes have shorthand forms:
| Shorthand | Matches | Equivalent class |
|---|---|---|
| `\d` | One digit | `[0-9]` |
| `\w` | One letter, digit, or underscore | `[0-9A-Za-z_]` |
| `\s` | One whitespace character (space, tab, newline) | `[ \t\n\r\f]` |
| `.` | Any one character except a newline | none |
The capital forms mean the opposite. \D matches a non-digit, \W a non-word character, and \S a non-space.
Shorthands also work inside brackets. [\w.+-] matches a word character, a dot, a plus sign, or a hyphen. Inside brackets, . and + are plain characters, so they need no escape. Put a - last (or first) in the set so it is not read as a range.
Quantifiers: how many times
A quantifier follows a piece and says how many times it may repeat.
| Quantifier | Meaning |
|---|---|
| `+` | One or more |
| `*` | Zero or more |
| `?` | Zero or one (optional) |
| `{3}` | Exactly 3 |
| `{2,4}` | Between 2 and 4 |
| `{2,}` | 2 or more |
A quantifier applies only to the piece directly before it. In \d{3}-\d{4}, the {3} applies to \d, and the hyphen is a literal character.
=REGEXEXTRACT("Call 555-0142 now", "\d{3}-\d{4}")This returns 555-0142.
Greedy and lazy matching
Quantifiers are greedy by default. They take as much text as they can while still letting the whole pattern match. This often surprises people.
=REGEXEXTRACT("<b>bold</b>", "<.+>")This returns <b>bold</b>, not <b>. The .+ runs to the end of the text, then backs up just far enough to find a closing >.
Add ? after a quantifier to make it lazy. A lazy quantifier takes as little as it can.
=REGEXEXTRACT("<b>bold</b>", "<.+?>")This returns <b>. A negated class often works better than a lazy dot. <[^>]+> also returns <b>, and it states the intent directly: "everything up to the next >".
Anchors and boundaries
Anchors match a position, not a character.
^matches the start of the text.$matches the end of the text.\bmatches a word boundary, the edge between a word character and a non-word character.
=REGEXEXTRACT("2026-11-27", "^\d{4}")This returns 2026. Without the ^, the pattern would match the first four digits found anywhere in the text. Here the result would be the same, but with text like ref 12 of 2026 the anchor changes the outcome. With the anchor, that text has no match at all.
Escaping special characters
Some characters have a special meaning in a pattern: . * + ? ( ) [ ] { } | ^ $ \. To match one literally, put a backslash before it.
=REGEXEXTRACT("Release v2.5 is out", "\d+\.\d+")This returns 2.5. Without the backslash, . would match any character, so the pattern would also accept 2x5.
In Sheets formulas, write the backslash once. The text "\d+" is correct. Do not double it to "\\d+" as you would in many programming languages.
Alternation and groups
The pipe | means "or". Parentheses group pieces together so a quantifier or an alternation applies to the whole group.
=REGEXEXTRACT("I have a Dog", "(?i)(cat|dog)")This returns Dog. Two things happen here.
(cat|dog)matches either word.(?i)at the start turns on case-insensitive matching. Matching is case-sensitive by default, so without itdogwould not matchDog.
Parentheses also create capture groups. When a pattern contains groups, REGEXEXTRACT returns what the groups matched instead of the whole match. This is why the result above is Dog, which is the group's text. A later chapter covers capture groups in detail. For now, remember that parentheses affect the output as well as the matching. If you only want grouping and no capture, write (?:cat|dog).
Reading a real pattern
Here is the email pattern from the topic's examples, taken apart piece by piece.
=REGEXEXTRACT("Contact: [email protected] today", "[\w.+-]+@[\w-]+\.[\w.]+")This returns [email protected].
[\w.+-]+matches one or more word characters, dots, plus signs, or hyphens. This is the part before the@.@matches itself.[\w-]+matches the first part of the domain, such asexample.\.matches a literal dot.[\w.]+matches the rest of the domain, such ascomorco.uk.
The match stops at the space after .com, because a space is not in any of the classes.
This pattern is practical, not strict. It accepts some strings that are not valid addresses, and it rejects a few rare valid ones. For cleaning data, that is usually the right trade-off. A fully correct email regex is long and not worth it here.
What RE2 does not support
RE2 guarantees fast matching by leaving out features that can be slow. These do not work in Sheets:
- Lookahead and lookbehind, such as
(?=...)and(?<=...). - Backreferences, such as
\1inside a pattern.
If a pattern from a web search uses these, it will produce an error. Rewrite it with a capture group and extract the group instead. For example, to get the digits that follow ID:, match ID:(\d+). The result is the digits only, because the group is returned and the ID: is not.
Testing a pattern with REGEXMATCH
REGEXMATCH(text, pattern) returns TRUE or FALSE. It is a quick way to check a pattern against a whole column before you extract anything. Fill a helper column with it and look for FALSE rows.
=ARRAYFORMULA(REGEXMATCH(A2:A10, "^\d{4}-\d{2}-\d{2}$"))This checks which cells contain exactly a date in YYYY-MM-DD form. The ^ and $ anchors force the pattern to match the whole cell. Without them, REGEXMATCH returns TRUE if the pattern appears anywhere in the cell.
Common mistakes
- Forgetting that matching is partial. A pattern matches anywhere in the text unless you add
^and$. - Using
.for a literal dot. In\d+.\d+, the dot matches any character. Write\.. - Using a greedy
.+between two markers. It runs past the first closing marker. Use.+?or a negated class. - Forgetting case.
[a-z]does not match capitals. Use[A-Za-z]or(?i). - Hidden spaces. A cell that looks like
ORD-1may have a trailing space. Patterns that use$then fail. ApplyTRIMto the input, or allow\s*before the$.
Exercise
A cell A2 contains the text "Invoice INV-2026-0042 due 30 days". Write a formula that returns only INV-2026-0042 using \d with quantifiers. Then explain why the pattern INV-.+ would be a poor choice.
Show answer
Use =REGEXEXTRACT(A2, "INV-\d{4}-\d{4}"). It returns INV-2026-0042. The pattern INV-.+ is greedy and the dot matches spaces, so it would run to the end of the cell and return "INV-2026-0042 due 30 days".
Exercise
What does =REGEXEXTRACT("price: $15.99 each", "\$\d+\.\d{2}") return, and why are two backslashes before non-letters needed?
Show answer
It returns $15.99. The first backslash makes $ a literal dollar sign, because an unescaped $ is an end-of-text anchor. The second makes . a literal dot, because an unescaped . matches any character. The \d+ matches 15 and \d{2} matches 99.