BigQuery REGEXP_CONTAINS vs REGEXP_EXTRACT: Which One, When?
BigQuery (GoogleSQL) gives you a family of RE2-powered regex functions. Two of them get all the attention — and the most confusion: REGEXP_CONTAINS answers “does it match?” while REGEXP_EXTRACT answers “give me the matching piece”. Below: the one-line decision table, exact outputs copied from Google’s documentation, and the ^..$ alternation footgun that silently makes every string “valid”.
TL;DR: REGEXP_CONTAINS(value, r're') → BOOL, partial (unanchored) match — TRUE if the pattern is found anywhere. For a full test: r'^(a|b)$'. REGEXP_EXTRACT(value, r're' [, position [, occurrence]]) → STRING or NULL; may contain at most one capture group (more → error). Write regex with raw strings r'...'. Regex engine = Google RE2 — no lookahead, no backreferences. NULL input → row is excluded from WHERE results, as usual.
The 10-second decision
| You want to… | Use | Returns | Header note |
|---|---|---|---|
| Filter rows by “matches / doesn’t match” | REGEXP_CONTAINS | BOOL (or NULL) | Partial match by default — see footgun below |
| Pull one piece of text out | REGEXP_EXTRACT | STRING or NULL | 1 capture group max (or zero) |
| All occurrences as an array | REGEXP_EXTRACT_ALL | ARRAY<STRING> | Like EXTRACT but “collect all matches” |
| Several capture groups in one go | REGEXP_EXTRACT_GROUPS | STRUCT | Preview — one field per group |
| Find the position of a match | REGEXP_INSTR | INT64 (1-based, 0 = no match) | Optional [, position [, occurrence]] |
| Scrub / normalize text | REGEXP_REPLACE | STRING | Replaces all matches with a literal replacement |
| Just a substring check (not regex) | CONTAINS_SUBSTR | BOOL | Case-insensitive, normalized — cheaper than RE2 |
REGEXP_CONTAINS — the WHERE workhorse
REGEXP_CONTAINS(value, regexp) returns TRUE or FALSE. It is a partial match: the pattern is searched anywhere in the value. From Google’s own examples:
| Value | Expression | Result |
|---|---|---|
foo@example.com | REGEXP_CONTAINS(value, r'@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+') | TRUE |
www.example.net | REGEXP_CONTAINS(value, r'@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+') | FALSE |
Because it is unanchored, a pattern like r'google' matches any string containing “google”. If your column holds event_page_path and you write WHERE REGEXP_CONTAINS(path, r'/blog'), you match /blog/, /blog/my-post and /foo/blogosphere. Decide on purpose: use r'^/blog' for “starts with”, r'^/\w+$' for “exact” semantics, and always parenthesize alternation.
The ^ and $ alternation footgun (the one that ruins WHERE)
Alternation (|) binds looser than concatenation, so a bare ^a|b$ means (^a)|(b$) — the anchors attach only to their nearest alternative. Google’s docs show exactly this with the email campaign-domains case:
| value | r'^([\w.+-]+@foo\.com|[\w.+-]+@bar\.org)$' | r'^[\w.+-]+@foo\.com|[\w.+-]+@bar\.org$' |
|---|---|---|
a@foo.com | true | true |
a@foo.computer | false | true |
b@bar.org | true | true |
!b@bar.org | false | true |
c@buz.net | false | false |
Rule: the moment your ^...$ contains a |, wrap the whole alternation in parentheses. Verify in the tester with the URL deep link — the unanchored search default reproduces BigQuery’s behavior column-for-column.
REGEXP_EXTRACT — capture text, get NULL on no match
REGEXP_EXTRACT(value, regexp[, position[, occurrence]])
- Returns the matching substring. If the pattern has no capture group, that is the whole match.
- If it has exactly one capture group, returns that group’s match (first match found, scanning left to right).
- If it has more than one capturing group → error. This is a compile-time failure, not a silent wrong result.
- No match → NULL — safe inside
WHERE/projections (NULL in WHERE simply excludes the row). positionandoccurrenceare 1-based.position = 0is invalid.occurrence > match count→ NULL.- By RE2 design: case-sensitive by default; use inline
(?i)for case-insensitive. Raw stringsr'...'avoid double backslashes everywhere.
Doc-verified EXTRACT semantics
| Call | Returns | Why |
|---|---|---|
REGEXP_EXTRACT('foo@example.com', r'^[a-zA-Z0-9_.+-]+') | foo | No group → whole match |
REGEXP_EXTRACT('foo@example.com', r'^[\w.+-]+@[a-zA-Z0-9-]+\.([a-zA-Z0-9-.]+$)') | com | One capture group → group contents |
REGEXP_EXTRACT('ab', '(.)b') | a | Grouping beats whole match when a group is present |
REGEXP_EXTRACT('xyztb', '(.)+b') | t | Greedy + — last single char before b wins |
REGEXP_EXTRACT('ab', '(z)?b') | NULL | b matched, but the optional group (z) never participated |
REGEXP_EXTRACT('anything', r'(a)(b)') | error | More than one capturing group → query fails |
The capture-group rule in practice
Worked examples (verified)
- Email prefix:
REGEXP_EXTRACT('foo@example.com', r'^[a-zA-Z0-9_.+-]+')→foo - UTM source:
REGEXP_EXTRACT('/lp?utm_medium=cpc&utm_source=google&utm_campaign=launch', r'utm_source=([^&]+)')→google - Domain from a URL:
REGEXP_EXTRACT('https://regextester.site/', r'(?:https?://)?([^/?#\s]+)')→regextester.site - Log level:
REGEXP_EXTRACT('ERROR [2026-11-13 22:00:01] svc: db timeout', r'\b(ERROR|WARN|INFO|DEBUG)\b')→ERROR - Row test:
SELECT COUNT(*) FROM `table` WHERE REGEXP_CONTAINS(user_email, r'^[\w.+-]+@(foo\.com|bar\.org)$') - Null-case audit:
SELECT * FROM `table` WHERE REGEXP_EXTRACT(event_page_path, r'utm_source=([^&]+)') IS NULL— finds rows where the pattern did not match (handy for broken UTM tagging).
Common errors, decoded
| Error | Root cause | Fix |
|---|---|---|
REGEXP_EXTRACT argument 2 is invalid / RE2 parse fails | Invalid regex (unbalanced brackets, stray backslashes in a non-raw string) | Use r'...'; test in a tester first |
regular expression has more than one capturing group | REGEXP_EXTRACT allows at most one (...) | Non-capturing (?:...) for structure; split into two calls; or use REGEXP_EXTRACT_GROUPS (preview) |
UNNEST returns empty / WHERE returns nothing | NULL from EXTRACT/CONTAINS in a WHERE → row excluded | COALESCE(expr, fallback) or swap the test |
| Everything “matches” your ^…$ test | Bare alternation | Parenthesize: ^(a|b)$ |
| Pattern works in GA4, fails here | GA4 matches the full value; BigQuery matches partially | See GA4 match types |
NULL propagation — the WHERE rule
GoogleSQL uses three-valued logic: if any input to a filter is NULL, the row is not returned. So REGEXP_CONTAINS(NULL, r'x') or REGEXP_EXTRACT(NULL, r'x') yields NULL, and the row silently disappears from WHERE. When your column can be NULL, wrap it: REGEXP_CONTAINS(COALESCE(path, ''), r'^/blog'), or accept NULL as an implicit exclusion and audit it with an explicit IS NULL comparison.
The full regex family at a glance
| Function | Returns | Notes |
|---|---|---|
REGEXP_CONTAINS | BOOL | Partial match, row filter |
REGEXP_EXTRACT | STRING / NULL | Synonym: REGEXP_SUBSTR |
REGEXP_EXTRACT_ALL | ARRAY<STRING> | All matches; count with ARRAY_LENGTH |
REGEXP_EXTRACT_GROUPS | STRUCT | Preview; one field per capture group |
REGEXP_REPLACE | STRING | Replaces all matches |
REGEXP_INSTR | INT64 | 1-based position, 0 = no match |
CONTAINS_SUBSTR | BOOL | Not regex — normalized substring |
FAQ
Does REGEXP_CONTAINS do a full match?
No — it is an unanchored partial match; TRUE means the pattern exists somewhere in the value. For exact semantics use r'^(pattern)$'. This is the opposite of GA4, where the entire value must match.
Why does REGEXP_EXTRACT return NULL when the text “obviously matches”?
Two reasons only: no match was found, or the (single allowed) capture group did not participate — e.g. REGEXP_EXTRACT('ab', '(z)?b') returns NULL because the match b never entered the group. Multiple capture groups are not a NULL case at all — the query errors instead.
Why does r'^[\w.+-]+@foo\.com|[\w.+-]+@bar\.org$' make everything valid?
Because | binds weakest: it parses as (^[\w.+-]+@foo\.com)|([\w.+-]+@bar\.org$), so any string starting with a plausible left half passes the first branch. Parenthesize the alternation: r'^([\w.+-]+@foo\.com|[\w.+-]+@bar\.org)$'.
Is BigQuery regex the same as GA4 regex?
Same engine (Google RE2), different anchoring: GA4 evaluates the whole value — wrap everything in ^…$ — while BigQuery is unanchored partial. The RE2 vs PCRE page lists what both engines reject (lookahead, backreferences); match types covers GA4’s per-surface quirks.
REGEXP_EXTRACT vs REGEXP_EXTRACT_ALL — which do I pick?
One value → EXTRACT (STRING or NULL). All matches/how many → EXTRACT_ALL (ARRAY). STRUCT of several groups → EXTRACT_GROUPS (preview). Need more than one group today: split into multiple EXTRACTs, or use REGEXP_REPLACE with a group-heavy pattern as a poor-man’s multi-capture.
Related on RegexLab: the GA4 regex match types guide (full vs partial per surface), RE2 vs PCRE (what the engine allows), the GA4 regex cheat sheet, extract domain regex, UTM parameter regex, email regex, log-level regex, and the pattern library (50+ verified recipes).