// strings reference

SQL REGEXP_MATCH function

Returns the capture groups of the first regex match in a string as a text array. Available since PostgreSQL 10.

Category
Strings
Returns
text[] or NULL
In this category
96 of 172

Syntax

regexp_match(string, pattern [, flags])

Engine support

EngineSupportNotes
SQL Server Different name or syntax SQL Server 2025's table-valued REGEXP_MATCHES returns every match, with groups as JSON.
PostgreSQL Yes Supported under this name.
MySQL Not built in No array result; REGEXP_SUBSTR returns one whole match.
SQLite Not built in No equivalent; the REGEXP operator only checks true or false.
Oracle Not built in No array result; call REGEXP_SUBSTR once per group with its group argument.

Examples

PostgreSQL

SELECT regexp_match('2026-11-20', '([0-9]+)-([0-9]+)-([0-9]+)') AS ymd;  -- {2026,11,20}

PostgreSQL

SELECT (regexp_match(email, '(.+)@(.+)'))[2] AS domain
FROM users;

Good to know

  • Without capture groups regexp_match returns a one-element array holding the whole match; with groups it returns one element per group.
  • No match returns NULL, not an empty array. A pattern that can match zero characters always matches: regexp_match('abc', 'x*') is {""}.
  • For every match, not just the first, use regexp_matches with the 'g' flag; it returns one row per match.

Questions about REGEXP_MATCH

How do I get all matches instead of the first?

Use regexp_matches(str, pattern, 'g'), which returns one row per match. regexp_match only looks at the first.

Why does regexp_match return an array instead of text?

Because it returns every capture group at once. Subscript the call to pick one: (regexp_match(str, pat))[2]. The parentheses around the call are required.

Does regexp_match use PCRE regex?

No. PostgreSQL uses its own Advanced Regular Expressions: POSIX extended syntax plus Perl-style escapes, so \d, \s, \w, lookahead, lookbehind and backreferences all work. Named groups and possessive quantifiers do not.

Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.

Cheat sheets for the engines that have it: SQL Server, PostgreSQL.

// fewer lookups

Functions your engine actually has

SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.