Request a demo

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.

GoogleSQL for BigQuery · Checked against the official reference in November 2026

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

ArgumentDescription
valueThe STRING or BYTES value to search.
regexpAn 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;
Result
order_idshashtagsnon_overlappingmatches_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;
Result
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;
Result
ticket_idincident_id
T-1INC-204
T-1INC-311

Common mistakes

  • Using two capturing groups to get key-value pairs. r'(\w+)=(\d+)' fails; extract the pairs with r'\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; write r'\d+'.
  • Treating an empty array as NULL. No match returns [], so IS NULL doesn't catch it; test ARRAY_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.

More string functions

All BigQuery string functions · All functions · BigQuery error messages

Observability for every BigQuery table, in about 15 minutes

Masthead detects freshness, volume and schema anomalies, monitors pipeline errors and maps column-level lineage across all your projects, from logs and metadata only.