All Products
Search
Document Center

PolarDB:REGEXP_SUBSTR

Last Updated:Mar 28, 2026

REGEXP_SUBSTR searches a string for a pattern specified by a POSIX regular expression and returns the matched substring.

Syntax

TEXT REGEXP_SUBSTR
(
  srcstr        TEXT,
  pattern       TEXT,
  position      INT  DEFAULT 1,
  occurrence    INT  DEFAULT 1,
  modifier      TEXT DEFAULT NULL,
  subexpression INT  DEFAULT 0
)

Parameters

ParameterDescription
srcstrThe string to search.
patternThe POSIX regular expression to match against.
positionThe character position in srcstr where the search starts. Default: 1 (the beginning of the string).
occurrenceWhich match to return when the pattern appears more than once. Default: 1 (the first match).
modifierFlags that control pattern matching behavior. Default: NULL. For supported values, see the PostgreSQL core documentation.
subexpressionIdentifies which parenthesized group in the pattern to return. Default: 0 (returns the full match). Subexpressions are numbered in the order of their opening parentheses. For example, subexpression => 2 returns the text captured by the second (...) group in the pattern. If you specify a value for subexpression, you must include one or more sets of parentheses in the pattern to isolate the portion of the value being searched.

Examples

Extract a substring by occurrence

The following examples search a phone number string for groups of three consecutive digits.

Return the first match (occurrence => 1):

polardb=# SELECT REGEXP_SUBSTR('800-555-****', '[0-9][0-9][0-9]', 1, 1) FROM DUAL;
 regexp_substr
---------------
 800
(1 row)

Return the second match (occurrence => 2):

polardb=# SELECT REGEXP_SUBSTR('800-555-****', '[0-9][0-9][0-9]', 1, 2) FROM DUAL;
 regexp_substr
---------------
 555
(1 row)