BigQuery · String function
BigQuery REGEXP_EXTRACT
REGEXP_EXTRACT(value, regex) returns the first substring that matches an RE2 regular expression, or the capture group if the pattern has one: REGEXP_EXTRACT(note, r'\d+') pulls the first number from text. Optional position and occurrence arguments pick a later match.
REGEXP_EXTRACT searches value for the regular expression and returns the matched text, or NULL when nothing matches. If the pattern contains one capturing group, only the text inside the group is returned, which is how you extract a value that follows a label: r'order_id=(\d+)' returns just the digits. A pattern with more than one capturing group is an error; turn extra groups into non-capturing ones with (?:...), or call the function once per value.
position sets the character where the search starts, and occurrence returns the nth match instead of the first (later matches start after the end of the previous one). Both must be positive. REGEXP_SUBSTR is a synonym with the same arguments.
Write patterns as raw strings, r'\d+'. In a normal quoted string, '\d' is an illegal escape sequence and the query doesn't compile; you would need '\\d'. 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. To get every match as an array, use REGEXP_EXTRACT_ALL.
Syntax
REGEXP_EXTRACT(value, regexp[, position[, occurrence]])
REGEXP_SUBSTR(value, regexp[, position[, occurrence]])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, usually written as a raw string such as r'\d+'. |
position | Optional INT64 start position, default 1. Must be positive; past the end of value returns NULL. |
occurrence | Optional INT64, default 1: which match to return. Must be positive; more than the number of matches returns NULL. |
Returns: STRING or BYTES, the same type as value. NULL if there is no match.
Examples
Extract a number from text
The third column uses a capturing group, so only the digits after ticket are returned.
SELECT
REGEXP_EXTRACT('Refund for order 58213, ticket 771', r'\d+') AS first_number,
REGEXP_EXTRACT('Refund for order 58213, ticket 771', r'\d+', 1, 2) AS second_number,
REGEXP_EXTRACT('Refund for order 58213, ticket 771', r'ticket (\d+)') AS ticket_id,
REGEXP_EXTRACT('Refund pending', r'\d+') AS no_match;| first_number | second_number | ticket_id | no_match |
|---|---|---|---|
| 58213 | 771 | 771 | NULL |
Parse URL parts with capture groups
SELECT
REGEXP_EXTRACT(url, r'^https?://([^/?#]+)') AS host,
REGEXP_EXTRACT(url, r'[?&]utm_source=([^&#]+)') AS utm_source,
REGEXP_EXTRACT(url, r'[?&]utm_term=([^&#]+)') AS utm_term
FROM (SELECT 'https://shop.example.com/cart?utm_source=newsletter&utm_medium=email' AS url);| host | utm_source | utm_term |
|---|---|---|
| shop.example.com | newsletter | NULL |
Extract and convert safely
WITH tickets AS (
SELECT 'T-1' AS ticket_id, 'Total: 1,234.50 EUR' AS body UNION ALL
SELECT 'T-2', 'Customer asked about delivery'
)
SELECT
ticket_id,
SAFE_CAST(REPLACE(REGEXP_EXTRACT(body, r'(\d[\d,]*\.\d{2})'), ',', '') AS NUMERIC) AS amount
FROM tickets
ORDER BY ticket_id;| ticket_id | amount |
|---|---|
| T-1 | 1234.5 |
| T-2 | NULL |
Common mistakes
- Writing the pattern in a normal string.
REGEXP_EXTRACT(s, '\d+')fails withIllegal escape sequence: \d; user'\d+'or'\\d+'. - Using two capturing groups.
r'(\d{4})-(\d{2})'fails withRegular expressions passed into extraction functions must not have more than 1 capturing group. Make one group non-capturing with(?:...), or extract each part separately. - Porting lookarounds from PostgreSQL, Snowflake or Python. RE2 rejects
(?=...)and(?<=...); capture the part you want instead, for exampler'price: (\d+)'rather thanr'(?<=price: )\d+'. - Casting the result with CAST. When there is no match the result is NULL and CAST returns NULL, but a partial match such as
12afails the query with a Bad int64 value error. Use SAFE_CAST. - Expecting Snowflake's group number argument.
REGEXP_SUBSTR(s, p, 1, 1, 'e', 2)has no BigQuery equivalent; put only the group you need in parentheses.
Cost and performance
- Regular expressions cost more CPU per row than LIKE, STARTS_WITH or STRPOS. On-demand pricing bills bytes scanned either way, but on capacity pricing the extra slot time is real. If you extract the same field in many queries, store it in its own column.
FAQ
How do I extract a number from a string in BigQuery?
REGEXP_EXTRACT(value, r'\d+') returns the first run of digits as a STRING. Wrap it in SAFE_CAST(... AS INT64). For decimals use r'\d+(?:\.\d+)?'.
How do I extract multiple capture groups at once?
REGEXP_EXTRACT allows only one group. Call it once per group, use REGEXP_EXTRACT_ALL for repeated matches, or split the string with SPLIT when it has a fixed delimiter.
Does BigQuery support lookahead in regex?
No. BigQuery uses RE2, which has no lookahead, lookbehind or backreferences. Use a capturing group around the part you want to return.
How do I make REGEXP_EXTRACT case-insensitive?
Start the pattern with the (?i) flag: REGEXP_EXTRACT(value, r'(?i)order (\d+)').
Related
Errors you may hit
More string functions
All BigQuery string functions · All functions · BigQuery error messages
