Regex in SQL
Regex in SQL depends on the database. PostgreSQL uses ~ (and ~* for case-insensitive) plus regexp_replace, regexp_matches and substring(col from pattern). MySQL 8 and MariaDB use REGEXP or RLIKE with REGEXP_REPLACE and REGEXP_SUBSTR. Oracle has REGEXP_LIKE. SQL Server has no regex before SQL Server 2025 and Azure SQL, which add REGEXP_LIKE.
Syntax
PostgreSQL: col ~ 'pattern' MySQL: col REGEXP 'pattern' Oracle: REGEXP_LIKE(col, 'pattern') BigQuery: REGEXP_CONTAINS(col, r'pattern')Examples
-- PostgreSQL
SELECT email FROM users WHERE email ~* '^[^\s@]+@[^\s@]+\.[^\s@]+$';Result: rows whose email has the usual shape, matched case-insensitively
-- MySQL 8
SELECT phone FROM contacts WHERE phone REGEXP '^[0-9]{3}-[0-9]{3}-[0-9]{4}$';Result: rows like 555-123-4567
-- PostgreSQL
SELECT regexp_replace('a b c', '\s+', ' ', 'g');Result: a b c
The 'g' flag replaces every run; without it only the first.
-- MySQL 8 / Oracle
SELECT REGEXP_SUBSTR('Order #4521', '[0-9]+');Result: 4521
Common mistakes
- Doubling nothing in MySQL: in a MySQL string literal, backslashes must be escaped, so write '\\d' or use [0-9], which works everywhere.
- Forgetting the 'g' flag in PostgreSQL regexp_replace, which replaces only the first match.
- Using LIKE as if it were regex; LIKE only has % and _ wildcards.
- Ignoring case rules: MySQL REGEXP follows the column collation (often case-insensitive), PostgreSQL ~ is case-sensitive unless you use ~*, Oracle REGEXP_LIKE takes an 'i' match parameter.
Practice Regex in SQL
These drills check your answer on live data, free, no account needed.
- Simple email addressesThe whole line must match. Use this simplified rule: a local part of letters, digits, dots, underscores, plus signs or hyphens; one @; one or more domain labels of letters, digits or hyphens separated by dots; and a final label of two or more letters.
- Keep only the digitsRemove every character that is not a digit, so phone numbers in any format become plain digits.
- US phone number formatsThe whole line must match. Accept (555) 123-4567, or ten digits split 3, 3 and 4 by the same separator throughout: a hyphen, a dot, or nothing.
Questions
How do I use regex in SQL Server?
SQL Server 2025 and Azure SQL Database add REGEXP_LIKE, REGEXP_REPLACE, REGEXP_SUBSTR and related functions. On earlier versions use LIKE with character ranges, PATINDEX, or a CLR function.
What are the regex functions in BigQuery?
REGEXP_CONTAINS(text, pattern) for a boolean, REGEXP_EXTRACT(text, pattern) for the first match or group, REGEXP_EXTRACT_ALL for an array, and REGEXP_REPLACE. BigQuery uses the RE2 flavor, so no lookarounds or backreferences.
Does SQLite support regex?
SQLite defines the REGEXP operator but ships no implementation; it works only if the host application registers a regexp() function (Python's sqlite3 can with create_function). Otherwise use LIKE or GLOB.
Related
- Regex in ExcelThe native regex functions in Microsoft 365 Excel to test, extract and replace text, the return_mode and case options, Google Sheets equivalents, and VBA.
- Regex in Python (re)How the Python re module works: search versus match versus fullmatch, findall and its group behavior, sub for replacement, raw strings, and flags.
- Regex for emailA practical email regex for JavaScript and other engines, how to validate a whole field, extract addresses from text, and why RFC-perfect patterns are a trap.
- Regex for numbers onlyThe ^\d+$ pattern for digits-only strings, stripping everything that is not a digit, extracting numbers from text, and patterns for signed and decimal values.