Function Reference

REGEXP_COUNT

Counts how many times a regular expression matches in a string.

PostgreSQL 17.9
REGEXP_COUNT(string, pattern[, start[, flags]])
  • [ ]optional part
string
String to search in
text
pattern
Regular expression
text
start
Position to start the search from, 1 by default
integerOptional
flags
Flags, for example i to ignore case
textOptional
bceimnpqstwx

Return value

An integer; 0 if there are no matches

Examples

Numbers and digits in a string

PostgreSQL 17.9
SELECT REGEXP_COUNT('a1b22c333', '[0-9]+') AS numbers,
	REGEXP_COUNT('a1b22c333', '[0-9]') AS digits;
numbersdigits
36

Vowels in names

PostgreSQL 17.9
SELECT member_name,
	REGEXP_COUNT(member_name, '[aeiou]', 1, 'i') AS vowels
FROM FamilyMembers;
member_namevowels
Headley Quincey6
Flavia Quincey6
Andie Quincey6
Lela Quincey5
Annie Quincey6
Ernest Forrest4
Constance Forrest5
Wednesday Addams5

Details

The function appeared in PostgreSQL 15. Matches do not overlap: in the string 'aaa' the pattern 'aa' is found once.

See also