regexp_count()

A function returning the number of occurrences of a regular expression
This entry relates to a PostgreSQL feature which is part of PostgreSQL 15, due to be released in late 2022.

regexp_count() is a system function returning the number of times a POSIX regular expression occurs in a string.

regexp_count() was added in PostgreSQL 15.

Usage

regexp_count ( string text, pattern text [, start integer [, flags text ] ] ) → integer

A list of flags is available in the PostgreSQL documentation: ARE Embedded-Option Letters. The flag 'g' is not supported.

Change history

Examples

Basic execution example for regexp_count():

postgres=# SELECT regexp_count('foobarboopzoo', 'o{1,}');
 regexp_count 
--------------
            3
(1 row)

Count the number of occurrences from the fourth character in the string:

postgres=# SELECT regexp_count('foobarboozoo', 'o{1,}', 4);
 regexp_count 
--------------
            2
(1 row)

Categories

String manipulation, System function

See also

regexp_instr(), regexp_like(), regexp_substr(), regexp_match(), regexp_matches(), regexp_replace()