BigQuery · String function
BigQuery REGEXP_REPLACE
REGEXP_REPLACE(value, regex, replacement) replaces every match of an RE2 regular expression: REGEXP_REPLACE(phone, r'[^0-9]', '') keeps only digits. In the replacement, \1 to \9 insert capture groups and \0 the whole match.
REGEXP_REPLACE finds all non-overlapping matches of the pattern and replaces each one. Use an empty replacement to delete matches, which makes it the most flexible way to remove characters: r'[^0-9]' strips everything except digits, r'\s+' with ' ' collapses runs of whitespace.
In the replacement, \1 to \9 insert the text of a capturing group and \0 inserts the whole match. Write the replacement as a raw string (r'\2, \1'); in a normal string you must double the backslashes ('\\2, \\1'). $1 has no special meaning and is inserted literally. Referring to a group that doesn't exist 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. The (?i) flag makes the pattern case-insensitive. A pattern that can match an empty string, such as r'x*', matches between every character, so anchor or tighten patterns like that. For plain text without patterns, REPLACE is simpler.
Syntax
REGEXP_REPLACE(value, regexp, replacement)Arguments and return value
| Argument | Description |
|---|---|
value | The STRING or BYTES value to change. |
regexp | An RE2 regular expression, usually a raw string such as r'\s+'. |
replacement | The replacement text. \1 to \9 insert capturing groups, \0 inserts the whole match. |
Returns: STRING or BYTES, the same type as value.
Examples
Remove and normalize characters
Collapsing whitespace keeps one space at each end; wrap the result in TRIM to remove them.
SELECT
REGEXP_REPLACE('+1 (415) 555-0100', r'[^0-9]', '') AS digits_only,
REGEXP_REPLACE(' too many spaces ', r'\s+', ' ') AS collapsed,
REGEXP_REPLACE('ERROR error Error', r'(?i)error', 'WARN') AS case_insensitive;| digits_only | collapsed | case_insensitive |
|---|---|---|
| 14155550100 | too many spaces | WARN WARN WARN |
Reorder text with backreferences
SELECT
REGEXP_REPLACE('Doe, Jane', r'^(\w+), (\w+)$', r'\2 \1') AS raw_string,
REGEXP_REPLACE('Doe, Jane', r'^(\w+), (\w+)$', '\\2 \\1') AS escaped_string,
REGEXP_REPLACE('2026-11-23', r'^(\d{4})-(\d{2})-(\d{2})$', r'\3.\2.\1') AS european_date,
REGEXP_REPLACE('order 42', r'\d+', r'#\0') AS whole_match;| raw_string | escaped_string | european_date | whole_match |
|---|---|---|---|
| Jane Doe | Jane Doe | 23.11.2026 | order #42 |
Mask emails
WITH users AS (
SELECT 'maria.garcia@example.com' AS email UNION ALL
SELECT 'li@example.org'
)
SELECT REGEXP_REPLACE(email, r'^(.)[^@]*(@.*)$', r'\1***\2') AS masked_email
FROM users
ORDER BY email;| masked_email |
|---|
| l***@example.org |
| m***@example.com |
Patterns that match empty strings
x* matches zero characters at every position, so a dash is inserted everywhere. Use x+ to replace only real matches.
SELECT REGEXP_REPLACE('abc', r'x*', '-') AS empty_matches;| empty_matches |
|---|
| -a-b-c- |
Common mistakes
- Using
$1for groups, as in JavaScript or Java. BigQuery inserts$1literally; writer'\1'. - Forgetting the raw string in the replacement.
'\1'in a normal string is an illegal escape sequence; user'\1'or'\\1'. - Using a backreference inside the pattern to find repeated characters.
r'(\w)\1'fails withinvalid escape sequence: \1because RE2 doesn't support backreferences in patterns. - Referencing a group the pattern doesn't have.
REGEXP_REPLACE(s, 'b', r'\1')fails withRewrite schema requests 1 matches, but the regexp only has 0 parenthesized subexpressions. - Expecting only the first match to be replaced. REGEXP_REPLACE replaces all matches; to change only the first, anchor the pattern (for example
r'^([^-]*)-'withr'\1_').
Cost and performance
- Regex replacement costs more CPU per row than REPLACE or TRANSLATE. When a fixed string or a character list is enough, use those; when the same cleanup runs in many queries, store the cleaned column.
FAQ
How do I remove special characters from a string in BigQuery?
Keep only what you allow: REGEXP_REPLACE(value, r'[^a-zA-Z0-9 ]', ''). Use \p{L} instead of a-zA-Z to keep accented and non-Latin letters.
How do I use capture groups in REGEXP_REPLACE?
Put parentheses around the parts in the pattern and reference them as \1, \2 and so on in a raw-string replacement: REGEXP_REPLACE(name, r'^(\w+) (\w+)$', r'\2, \1').
How do I replace text case-insensitively in BigQuery?
Add (?i) at the start of the pattern: REGEXP_REPLACE(value, r'(?i)colour', 'color').
What is the difference between REGEXP_REPLACE and REPLACE?
REPLACE swaps a literal substring and is case-sensitive. REGEXP_REPLACE matches a regular expression and can reuse captured text.
Related
More string functions
All BigQuery string functions · All functions · BigQuery error messages
