BigQuery · String function
BigQuery REGEXP_CONTAINS
REGEXP_CONTAINS(value, regex) returns TRUE if any part of the string matches an RE2 regular expression: REGEXP_CONTAINS(email, r'@example\.(com|org)$'). Use it when LIKE's % and _ wildcards are not enough; anchor with ^ and $ for a full match.
REGEXP_CONTAINS tests a string against a regular expression and returns a BOOL. It looks for a match anywhere in the value, so r'error' matches 'disk error on node 3'. To require the whole value to match, anchor the pattern and wrap alternatives in a group: r'^(pending|paid)$'. Without the group, ^pending|paid$ matches values that start with pending or end with paid.
Compared with LIKE: LIKE always matches the whole string and only knows % (any sequence) and _ (one character). REGEXP_CONTAINS supports alternation, character classes, repetition counts, anchors and the (?i) case-insensitive flag. Both are case-sensitive by default. Use LIKE, STARTS_WITH or CONTAINS_SUBSTR for simple checks, and REGEXP_CONTAINS when you need a real pattern.
A NULL value or pattern returns NULL, and an invalid pattern is an error. BigQuery uses the RE2 engine, which guarantees linear-time matching but doesn't support lookaheads, lookbehinds ((?=, (?!, (?<=) or backreferences inside the pattern ((\w)\1); those fail with Cannot parse regular expression.
Syntax
REGEXP_CONTAINS(value, regexp)Arguments and return value
| Argument | Description |
|---|---|
value | The STRING or BYTES value to test. |
regexp | An RE2 regular expression, usually a raw string such as r'^\d{5}$'. |
Returns: BOOL. NULL if either argument is NULL.
Examples
REGEXP_CONTAINS vs LIKE
SELECT
REGEXP_CONTAINS('Payment FAILED: card declined', r'(?i)failed') AS regex_any_case,
'Payment FAILED: card declined' LIKE '%failed%' AS like_case_sensitive,
LOWER('Payment FAILED: card declined') LIKE '%failed%' AS like_lowercased,
REGEXP_CONTAINS('Payment FAILED: card declined', r'declined|expired') AS alternation;| regex_any_case | like_case_sensitive | like_lowercased | alternation |
|---|---|---|---|
| true | false | true | true |
Partial match vs full match
SELECT
code,
REGEXP_CONTAINS(code, r'\d{5}') AS contains_5_digits,
REGEXP_CONTAINS(code, r'^\d{5}$') AS is_exactly_5_digits
FROM UNNEST(['10115', '10115-1234', 'A1011']) AS code
ORDER BY code;| code | contains_5_digits | is_exactly_5_digits |
|---|---|---|
| 10115 | true | true |
| 10115-1234 | true | false |
| A1011 | false | false |
Flag invalid emails
WITH signups AS (
SELECT 'maria@example.com' AS email UNION ALL
SELECT 'maria@example' UNION ALL
SELECT 'not an email'
)
SELECT email, REGEXP_CONTAINS(email, r'^[\w.+-]+@[\w-]+(\.[\w-]+)+$') AS looks_valid
FROM signups
ORDER BY email;| looks_valid | |
|---|---|
| maria@example | false |
| maria@example.com | true |
| not an email | false |
Common mistakes
- Forgetting that the match is partial.
REGEXP_CONTAINS(zip, r'\d{5}')accepts10115-1234; anchor withr'^\d{5}$'. - Anchoring alternatives without a group.
r'^paid|refunded$'matchespaid_lateandnot refunded; writer'^(paid|refunded)$'. - Using LIKE wildcards in a regex.
%is a literal percent sign in a regular expression; the equivalent of%is.*, and of_is.. An unescaped.matches any character, so escape it in domains:r'example\.com'. - Looking for ILIKE, RLIKE or
~. BigQuery has none of them: useREGEXP_CONTAINS(col, r'(?i)pattern')orLOWER(col) LIKE '%pattern%'. - Porting lookaheads, for example a password rule
(?=.*\d). RE2 rejects them; split the rule into several REGEXP_CONTAINS conditions joined with AND.
Cost and performance
- Regular expressions need more CPU per row than LIKE, STARTS_WITH or
=. When a prefix or substring check is enough, the simpler form is cheaper in slot time. - On tables with a search index,
=,IN,LIKE 'prefix%', STARTS_WITH and ENDS_WITH can use the index. REGEXP_CONTAINS is not in the documented list, so it always evaluates every row it reads.
FAQ
What is the difference between LIKE and REGEXP_CONTAINS in BigQuery?
LIKE matches the whole string using % and _ wildcards. REGEXP_CONTAINS finds a regular expression anywhere in the string, supports alternation, classes and (?i), and needs ^...$ for a full match. Both are case-sensitive by default.
How do I do a case-insensitive LIKE in BigQuery?
Use LOWER(col) LIKE '%text%', REGEXP_CONTAINS(col, r'(?i)text'), or CONTAINS_SUBSTR(col, 'text'), which ignores case by default.
How do I match one of several patterns?
Use alternation, REGEXP_CONTAINS(col, r'refund|chargeback'), or the quantified LIKE operator, col LIKE ANY ('%refund%', '%chargeback%').
Is REGEXP_CONTAINS slow in BigQuery?
It costs more CPU per row than LIKE, but on-demand billing depends on bytes scanned, not on the functions used. Select fewer columns and filter partitions first.
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
