All Products
Search
Document Center

PolarDB:REGEXP_COUNT

Last Updated:Mar 28, 2026

REGEXP_COUNT searches a string for a regular expression pattern and returns the number of times the pattern occurs.

Syntax

INTEGER REGEXP_COUNT
(
  srcstr    TEXT,
  pattern   TEXT,
  position  DEFAULT 1,
  modifier  DEFAULT NULL
)

Parameters

ParameterTypeDefaultDescription
srcstrTEXTThe string to search.
patternTEXTThe regular expression pattern to search for.
positionINTEGER1The character position in srcstr at which to start searching.
modifierTEXTNULLOne or more flags that control pattern matching behavior.
For the full list of supported modifiers, see Pattern Matching in the PostgreSQL documentation.

Examples

Count occurrences starting from position 1

The following example counts how many times the letter i appears in 'reinitializing', starting from the first character:

polardb=# SELECT REGEXP_COUNT('reinitializing', 'i', 1) FROM DUAL;
 regexp_count
--------------
            5
(1 row)

Count occurrences starting from position 6

To start counting from a position other than the beginning, specify the position parameter. This example counts occurrences of i starting from position 6, so matches before that position are excluded:

polardb=# SELECT REGEXP_COUNT('reinitializing', 'i', 6) FROM DUAL;
 regexp_count
--------------
            3
(1 row)