Request a demo

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.

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

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

ArgumentDescription
valueThe STRING or BYTES value to change.
regexpAn RE2 regular expression, usually a raw string such as r'\s+'.
replacementThe 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;
Result
digits_onlycollapsedcase_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;
Result
raw_stringescaped_stringeuropean_datewhole_match
Jane DoeJane Doe23.11.2026order #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;
Result
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;
Result
empty_matches
-a-b-c-

Common mistakes

  • Using $1 for groups, as in JavaScript or Java. BigQuery inserts $1 literally; write r'\1'.
  • Forgetting the raw string in the replacement. '\1' in a normal string is an illegal escape sequence; use r'\1' or '\\1'.
  • Using a backreference inside the pattern to find repeated characters. r'(\w)\1' fails with invalid escape sequence: \1 because RE2 doesn't support backreferences in patterns.
  • Referencing a group the pattern doesn't have. REGEXP_REPLACE(s, 'b', r'\1') fails with Rewrite 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'^([^-]*)-' with r'\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.

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.