Sub topic 7
Just Enough Regular Expressions
Assume you can enter formulas, reference cells and ranges, and use functions such as TRIM and IFERROR. This chapter introduces regular expressions for use with REGEXEXTRACT. A regular expression, often shortened to regex, is a pattern that describes text you want to find.
Google Sheets uses the RE2 regular expression syntax. You can use regex to find an email address, check an ID format, or extract a number from a longer value.
The working model
A regex pattern is compared with a text value. Some characters match themselves. Other characters have special meanings.
For example, the pattern ORD-\d+ means:
ORD-must appear exactly as written.\dmatches one digit.+means “one or more of the previous item.”
The pattern matches ORD-10492 in a cell containing Order: ORD-10492.
=REGEXEXTRACT(A2, "ORD-\d+")The result is text, even though the final part contains digits. Convert it later with VALUE if you need to calculate with it.
Common pattern parts
| Pattern | Meaning | Example matches |
|---|---|---|
| `.` | Any single character | `a`, `7`, `-` |
| `\d` | One digit from 0 through 9 | `4` |
| `\w` | A word character, such as a letter, digit, or underscore | `A`, `7`, `_` |
| `\s` | Whitespace, such as a space or tab | a space |
| `[A-Z]` | One uppercase letter from A through Z | `Q` |
| `[0-9]` | One digit from 0 through 9 | `8` |
| `[^,]` | One character that is not a comma | `a`, `7` |
| `*` | Zero or more of the previous item | `ab`, `abbb` |
| `+` | One or more of the previous item | `a`, `abbb` |
| `?` | Zero or one of the previous item | `a`, `ab` |
| `{3}` | Exactly three of the previous item | `ABC` |
| `{2,5}` | Between two and five of the previous item | `12`, `12345` |
| `^` | Start of the text | beginning only |
| `$` | End of the text | ending only |
A character class uses square brackets. The pattern [A-Z0-9]+ matches one or more uppercase letters or digits. A caret inside the opening bracket reverses the class, so [^,]+ matches one or more characters that are not commas.
Match a whole value
Without anchors, a pattern can match a substring inside a larger value. Add ^ and $ when the entire cell must follow a format.
This formula checks whether A2 contains exactly three uppercase letters, a hyphen, and four digits:
=REGEXMATCH(A2, "^[A-Z]{3}-[0-9]{4}$")For example, ABC-1042 matches. Order ABC-1042 does not, because extra text appears before the required pattern.
Literal characters and escaping
Some characters have special meanings in regex. These include ., +, *, ?, (, ), [, and ]. To match one of these characters literally, put a backslash before it.
For example, this pattern finds a value such as v2.5:
=REGEXEXTRACT(A2, "v[0-9]+\.[0-9]+")The period is escaped because an unescaped period means “any character.”
Common errors
- Using
\d+when the value must contain only digits. Add^and$for a full-cell check. - Forgetting that REGEXMATCH returns TRUE or FALSE, while REGEXEXTRACT returns the matching text.
- Expecting regex to support every regular expression feature. RE2 does not support backreferences or lookbehind assertions.
- Treating a regex pattern as ordinary text. Special characters such as
.and+may change the match. - Confusing
\swith a normal space. Use a literal space when only an ordinary space should match.
Exercise
What pattern matches exactly two uppercase letters, a hyphen, and three digits, with no extra text?
Show answer
^[A-Z]{2}-[0-9]{3}$