PostgreSQL 18 reference · String Functions

PostgreSQL REGEXP_MATCH() Function

Return the captured groups of the first regular-expression match as a text array, or NULL if there's no match.

PostgreSQL 18 Since 10

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

regexp_match(string, pattern, flags)

Parameters

ParameterDescription
string The input text.
pattern A POSIX regular expression.
flags optional For example i.

Return value

A text[] array, or NULL.

Examples

Capture groups
SELECT regexp_match('Order #A-1042', '#([A-Z])-(\d+)') AS parts;

Output

  parts
----------
 {A,1042}
(1 row)

Run this on your computer — see “Get Started” for setup.

Take one group
SELECT email, (regexp_match(email, '@(.*)$'))[1] AS domain
FROM customers
WHERE email IS NOT NULL
LIMIT 2;

Output

      email       |   domain
------------------+-------------
 [email protected]  | example.com
 [email protected] | example.com
(2 rows)

Run this on your computer — see “Get Started” for setup.

regexp_matches(string, pattern, 'g') returns one row per match instead; regexp_like() (15+) just tests for a match.

Version history

VersionChange
PostgreSQL 10Added

Was this page helpful?