Strings

REGEXP_LIKE

Checks whether a string matches a regular expression.

Syntax

REGEXP_LIKE(<text>, '<pattern>')
REGEXP_LIKE(<text>, '<pattern>', '<flags>')
<text> ~ '<pattern>'
<text> ~* '<pattern>'

Returns true or false. Unlike LIKE with its % and _, the pattern is a full regular expression: '^[0-9]+$' — digits only, '@' — there's an at sign somewhere. The 'i' flag turns off case sensitivity. The operators ~ and ~* (case-insensitive) do the same.

Data for the examples

employees

namedepartmentsalarybonusphone
Emmabackend1200300+44 7700 900123
Liambackend1000NULLNULL
Oliviafrontend1350150+44 7700 900456
Noahfrontend950NULL+44 7700 900789
Sophiadesign1200200NULL

These tables already exist in the sandbox — the examples below use them. Want to try this in your own database? Copy the SQL.

Digits only or not
SELECT REGEXP_LIKE('12345', '^[0-9]+$') AS only_digits,
REGEXP_LIKE('12a45', '^[0-9]+$') AS with_letter;
only_digitswith_letter
truefalse
Is the phone number format correct
SELECT name, phone,
REGEXP_LIKE(phone, '^\+44 [0-9]{4} [0-9]{6}$') AS valid_format
FROM employees;
namephonevalid_format
Emma+44 7700 900123true
LiamNULLNULL
Olivia+44 7700 900456true
Noah+44 7700 900789true
SophiaNULLNULL

\+ is the plus sign itself, [0-9]{4} — exactly four digits.

The same with an operator
SELECT name
FROM employees
WHERE name ~* '^[a-e]';
name
Emma

~* ignores case, so the capital E in Emma matches [a-e].

Watch out

The pattern looks for a match anywhere in the string: REGEXP_LIKE('abc123', '[0-9]') gives true. To check the whole string, add ^ at the start and $ at the end.

Plus signs, dots and brackets have special meanings. To match the character itself, put a backslash in front of it: '\+'.

REGEXP_LIKE exists only since PostgreSQL 15. In older versions, use the ~ operator.