Function Reference

REGEXP_MATCH

MySQL equivalent:

Returns an array of substrings from the first match of a regular expression.

PostgreSQL 17.5
REGEXP_MATCH(string, pattern[, flags])

Parameters

stringtext
String to search in
patterntext
Regular expression
flagstextoptional
Flags, for example i to ignore case

Return value

A text array (text[]) from the first match; NULL if there is no match

Examples

Date parts from text

PostgreSQL 17.5
SELECT REGEXP_MATCH(
		'Order from 2024-03-08',
		'([0-9]{4})-([0-9]{2})-([0-9]{2})'
	);
regexp_match
20240308

Username and domain from email

PostgreSQL 17.5
SELECT email,
	REGEXP_MATCH(email, '^(.+)@(.+)$') AS parts
FROM Users
LIMIT 5;
emailparts
barjam@hotmail.combarjamhotmail.com
tellis@me.comtellisme.com
metzzo@superhotmail.commetzzosuperhotmail.com
ralamosm@verizon.netralamosmverizon.net
barjam@hotmail.combarjamhotmail.com

Domain as an array element

PostgreSQL 17.5
SELECT email,
	(REGEXP_MATCH(email, '@(.+)$')) [1] AS domain
FROM Users
LIMIT 5;
emaildomain
barjam@hotmail.comhotmail.com
tellis@me.comme.com
metzzo@superhotmail.comsuperhotmail.com
ralamosm@verizon.netverizon.net
barjam@hotmail.comhotmail.com

Details

If the pattern contains groups in parentheses, the array holds one element per group, otherwise a single element with the whole match. A single element is taken by its number: (REGEXP_MATCH(...))[1]. The g flag is not supported: all matches are returned by REGEXP_MATCHES(..., 'g'), one result row per match. To get the match as a string rather than an array, use REGEXP_SUBSTR, available in PostgreSQL since version 15.

See also