Advertisement

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…UseReturnsHeader note
Filter rows by “matches / doesn’t match”REGEXP_CONTAINSBOOL (or NULL)Partial match by default — see footgun below
Pull one piece of text outREGEXP_EXTRACTSTRING or NULL1 capture group max (or zero)
All occurrences as an arrayREGEXP_EXTRACT_ALLARRAY<STRING>Like EXTRACT but “collect all matches”
Several capture groups in one goREGEXP_EXTRACT_GROUPSSTRUCTPreview — one field per group
Find the position of a matchREGEXP_INSTRINT64 (1-based, 0 = no match)Optional [, position [, occurrence]]
Scrub / normalize textREGEXP_REPLACESTRINGReplaces all matches with a literal replacement
Just a substring check (not regex)CONTAINS_SUBSTRBOOLCase-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:

ValueExpressionResult
foo@example.comREGEXP_CONTAINS(value, r'@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+')TRUE
www.example.netREGEXP_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:

valuer'^([\w.+-]+@foo\.com|[\w.+-]+@bar\.org)$'r'^[\w.+-]+@foo\.com|[\w.+-]+@bar\.org$'
a@foo.comtruetrue
a@foo.computerfalsetrue
b@bar.orgtruetrue
!b@bar.orgfalsetrue
c@buz.netfalsefalse

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]])

Doc-verified EXTRACT semantics

CallReturnsWhy
REGEXP_EXTRACT('foo@example.com', r'^[a-zA-Z0-9_.+-]+')fooNo group → whole match
REGEXP_EXTRACT('foo@example.com', r'^[\w.+-]+@[a-zA-Z0-9-]+\.([a-zA-Z0-9-.]+$)')comOne capture group → group contents
REGEXP_EXTRACT('ab', '(.)b')aGrouping beats whole match when a group is present
REGEXP_EXTRACT('xyztb', '(.)+b')tGreedy + — last single char before b wins
REGEXP_EXTRACT('ab', '(z)?b')NULLb matched, but the optional group (z) never participated
REGEXP_EXTRACT('anything', r'(a)(b)')errorMore than one capturing group → query fails

The capture-group rule in practice

-- OK: exactly ONE capture group SELECT REGEXP_EXTRACT(event_page_path, r'utm_source=([^&]+)') AS source FROM `analytics_456.events_*` -- OK: no groups at all SELECT REGEXP_EXTRACT(user_email, r'^[a-z]+') AS prefix -- FAILS: TWO groups → "regular expression has more than one capturing group" SELECT REGEXP_EXTRACT(event_data, r'([a-z]+)@([a-z.]+)')

Worked examples (verified)

  1. Email prefix: REGEXP_EXTRACT('foo@example.com', r'^[a-zA-Z0-9_.+-]+') → foo
  2. UTM source: REGEXP_EXTRACT('/lp?utm_medium=cpc&utm_source=google&utm_campaign=launch', r'utm_source=([^&]+)') → google
  3. Domain from a URL: REGEXP_EXTRACT('https://regextester.site/', r'(?:https?://)?([^/?#\s]+)') → regextester.site
  4. Log level: REGEXP_EXTRACT('ERROR [2026-11-13 22:00:01] svc: db timeout', r'\b(ERROR|WARN|INFO|DEBUG)\b') → ERROR
  5. Row test: SELECT COUNT(*) FROM `table` WHERE REGEXP_CONTAINS(user_email, r'^[\w.+-]+@(foo\.com|bar\.org)$')
  6. 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

ErrorRoot causeFix
REGEXP_EXTRACT argument 2 is invalid / RE2 parse failsInvalid regex (unbalanced brackets, stray backslashes in a non-raw string)Use r'...'; test in a tester first
regular expression has more than one capturing groupREGEXP_EXTRACT allows at most one (...)Non-capturing (?:...) for structure; split into two calls; or use REGEXP_EXTRACT_GROUPS (preview)
UNNEST returns empty / WHERE returns nothingNULL from EXTRACT/CONTAINS in a WHERE → row excludedCOALESCE(expr, fallback) or swap the test
Everything “matches” your ^…$ testBare alternationParenthesize: ^(a|b)$
Pattern works in GA4, fails hereGA4 matches the full value; BigQuery matches partiallySee 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

FunctionReturnsNotes
REGEXP_CONTAINSBOOLPartial match, row filter
REGEXP_EXTRACTSTRING / NULLSynonym: REGEXP_SUBSTR
REGEXP_EXTRACT_ALLARRAY<STRING>All matches; count with ARRAY_LENGTH
REGEXP_EXTRACT_GROUPSSTRUCTPreview; one field per capture group
REGEXP_REPLACESTRINGReplaces all matches
REGEXP_INSTRINT641-based position, 0 = no match
CONTAINS_SUBSTRBOOLNot regex — normalized substring
Test your pattern before shipping the query
BigQuery matches like the tester’s default unanchored search. Check the i flag for (?i) case-insensitivity — GA4, by contrast, is full-match, see the match types guide.
Open the regex tester →

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).