BigQuery · String function
BigQuery REGEXP_EXTRACT_ALL
REGEXP_EXTRACT_ALL(value, regex) returns an ARRAY of every non-overlapping match of an RE2 regular expression: REGEXP_EXTRACT_ALL(text, r'#(\w+)') returns all hashtags. It returns an empty array when nothing matches.
REGEXP_EXTRACT_ALL scans the whole string and collects each match in order. If the pattern has a capturing group, the array holds the group's text instead of the full match; more than one capturing group is an error. Matches never overlap: after a match, the search resumes at the end of the full match, so 'banana' contains only one ana.
No match returns an empty array, and NULL input returns a NULL array (shown as [] in results). Unlike REGEXP_EXTRACT, there are no position or occurrence arguments; index the array with [SAFE_OFFSET(n)] or count matches with ARRAY_LENGTH.
Because SPLIT only accepts a literal delimiter, REGEXP_EXTRACT_ALL is also how you split on a pattern: extract the parts you want to keep, such as r'[^,;]+' for values separated by commas or semicolons. 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_EXTRACT_ALL(value, regexp)Arguments and return value
| Argument | Description |
|---|---|
value | The STRING or BYTES value to search. |
regexp | An RE2 regular expression with zero or one capturing group. |
Returns: ARRAY<STRING> or ARRAY<BYTES>. Empty if there is no match; NULL if value is NULL.
Examples
All numbers, hashtags and non-overlapping matches
SELECT
REGEXP_EXTRACT_ALL('Shipped orders 1042, 1043 and 1050', r'\d+') AS order_ids,
REGEXP_EXTRACT_ALL('Loving the new #bigquery #sql features', r'#(\w+)') AS hashtags,
REGEXP_EXTRACT_ALL('banana', 'ana') AS non_overlapping,
ARRAY_LENGTH(REGEXP_EXTRACT_ALL('no digits here', r'\d+')) AS matches_when_none;| order_ids | hashtags | non_overlapping | matches_when_none |
|---|---|---|---|
| [1042, 1043, 1050] | [bigquery, sql] | [ana] | 0 |
Split on several delimiters
Extracting runs of allowed characters also drops empty parts and surrounding spaces, which SPLIT would keep.
SELECT REGEXP_EXTRACT_ALL('gift; express,fragile ;;returns', r'[^,;\s]+') AS tags;| tags |
|---|
| [gift, express, fragile, returns] |
One row per extracted value
T-2 has an empty array, so the comma join drops it. Use LEFT JOIN UNNEST(...) to keep it.
WITH tickets AS (
SELECT 'T-1' AS ticket_id, 'Duplicate of INC-204 and INC-311' AS body UNION ALL
SELECT 'T-2', 'No related incidents'
)
SELECT ticket_id, incident_id
FROM tickets, UNNEST(REGEXP_EXTRACT_ALL(body, r'INC-\d+')) AS incident_id
ORDER BY ticket_id, incident_id;| ticket_id | incident_id |
|---|---|
| T-1 | INC-204 |
| T-1 | INC-311 |
Common mistakes
- Using two capturing groups to get key-value pairs.
r'(\w+)=(\d+)'fails; extract the pairs withr'\w+=\d+'and then SPLIT each on=. - Expecting overlapping matches.
REGEXP_EXTRACT_ALL('banana', 'ana')returns one element. With a capturing group, the next search still starts after the whole match, not after the group. - Writing the pattern without a raw string.
'\d+'is an illegal escape sequence; writer'\d+'. - Treating an empty array as NULL. No match returns
[], soIS NULLdoesn't catch it; testARRAY_LENGTH(...) = 0.
Cost and performance
- Each regex call scans the whole string, and Google's compute best practices note that regular expressions need extra computation. If you extract the same values in many queries, store the array in a column.
FAQ
How do I extract all numbers from a string in BigQuery?
REGEXP_EXTRACT_ALL(value, r'\d+') returns an array of digit strings. Convert them with ARRAY(SELECT CAST(x AS INT64) FROM UNNEST(...) AS x).
How do I split a string by a regular expression in BigQuery?
SPLIT doesn't accept patterns. Extract the pieces you want to keep instead: REGEXP_EXTRACT_ALL(value, r'[^,;|]+') splits on commas, semicolons and pipes.
How do I count regex matches?
ARRAY_LENGTH(REGEXP_EXTRACT_ALL(value, r'pattern')). It returns 0 when nothing matches and NULL when the value is NULL.
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
