Updated on 2026-11-06 GMT+08:00

regexp_extract

Matches the string source against the grouping rules of pattern and returns the string content matched by the groupid-th group.

Syntax

regexp_extract(string <source>, string <pattern>[, bigint <groupid>]) 

Parameters

Parameter

Mandatory

Type

Description

source

Yes

STRING

The string to be matched.

pattern

Yes

STRING

The regular expression. The pattern to be matched.

groupid

No

BIGINT

A BIGINT type constant, must be greater than or equal to 0. When not specified, the default is 1, meaning the first group is returned. If groupid is 0, the substring matching the entire pattern is returned.

Return Values

Returns a value of type STRING.

  • If pattern is an empty string or pattern contains no groups, an error is returned.
  • If groupid is not of type BIGINT or is less than 0, an error is reported.
  • If source, pattern, or groupid is NULL, NULL is returned.

Example Code

Split basketball by bas(.*?)(ball) and return ket.

select regexp_extract('basketball', 'bas(.*?)(ball)'); 

Returns basketball.

select regexp_extract('basketball', 'bas(.*?)(ball)', 0); 

Returns 99. In SQL, two \ characters are needed as escape characters.

select regexp_extract('8d99d8', '8d(\\d+)d8');