Regular expressions in SQL
SQL engines support regular expressions through functions and operators like REGEXP_MATCHES, REGEXP_REPLACE, and SIMILAR TO for pattern-based text matching and extraction.
Overview
While LIKE handles simple wildcard matching, regular expressions (regex) let SQL queries match, extract, and rewrite text using far more expressive patterns — character classes, quantifiers, alternation, and capture groups. Most SQL engines expose regex through a family of functions rather than a single operator.
Common patterns
Copy code
-- Does the column match a pattern?
SELECT * FROM users WHERE REGEXP_MATCHES(email, '^[\w.+-]+@[\w-]+\.[a-z]{2,}$');
-- Extract the first capture group
SELECT REGEXP_EXTRACT(url, 'https?://([^/]+)/', 1) AS host FROM page_views;
-- Replace matches
SELECT REGEXP_REPLACE(phone, '[^0-9]', '', 'g') AS digits_only FROM contacts;
The standard SQL SIMILAR TO operator offers a lighter-weight, POSIX-flavored alternative to full regex functions, but is less commonly used than dedicated REGEXP_* functions in modern engines.
DuckDB specifics
DuckDB provides a rich, PCRE-based (via RE2) regex toolkit: regexp_matches(string, pattern) for boolean matching, regexp_replace(string, pattern, replacement, options) for substitution (with a 'g' option flag for global replace), regexp_extract(string, pattern, group) for pulling out one capture group, and regexp_extract_all(string, pattern, group) — a DuckDB convenience that returns every match as a LIST rather than just the first:
Copy code
SELECT regexp_extract_all('order-12, order-45, order-99', '\d+') AS order_ids;
-- ['12', '45', '99']
DuckDB also supports the Postgres-style ~ (matches) and !~ (does not match) operators as shorthand for regexp_matches, and SIMILAR TO for lighter pattern matching. Because DuckDB's regex engine is RE2-based, it does not support backreferences in patterns, unlike PCRE-based engines.
Related terms
The LIKE operator performs pattern matching on string values, using % as a wildcard for any number of characters and _ for a single character.
String functions →String functions manipulate text values in SQL — concatenating, trimming, searching, splitting, and reformatting strings as part of a query.
Glob pattern →A glob pattern is a wildcard-based string pattern, using characters like `*` and `?`, used to match multiple filenames or paths at once instead of listing them individually.
SQL function →A SQL function is a reusable piece of code that performs a specific operation and returns a result.
SQL analytics →SQL analytics refers to using SQL queries to analyze data and derive insights, typically working with large datasets stored in databases or data warehouses.
SQL →SQL (Structured Query Language) is the standard language for working with relational databases.
FAQS
LIKE supports only two wildcards (% for any sequence of characters, _ for a single character). Regular expressions, accessed through functions like REGEXP_MATCHES or REGEXP_REPLACE, support character classes, quantifiers, alternation, and capture groups for far more precise matching.
Use regexp_extract_all(string, pattern), which returns every match (or every match of a specific capture group) as a LIST, rather than regexp_extract, which returns only the first match.
