REGEXP_EXTRACT
The REGEXP_EXTRACT function searches the source string for a match to the regular expression pattern and returns the content of the group specified by group_id.
Usage notes
The REGEXP_EXTRACT function follows Java regular expression specifications for the Hive-compatible data type edition. For the 1.0 and 2.0 data type editions, it follows MaxCompute specifications.
Syntax
STRING REGEXP_EXTRACT(STRING <source>, STRING <pattern>[, BIGINT <group_id>])Parameters
source: Required. A value of the STRING type. This parameter specifies the string to be searched.
pattern: Required. A constant or regular expression of the STRING type. This parameter specifies the pattern for matching. For more information about how to write regular expressions, see Regular expression specifications.
group_id: Optional. A value of the BIGINT type. The value must be greater than or equal to 0.
If group_id is not specified, the default value is 1, and the function returns the first matched group.
If group_id is specified, the function returns the content of the matched group group_id.
If group_id is 0, the function returns the substring that matches the entire pattern.
Data is stored in UTF-8 format. The hexadecimal code range for Chinese characters is [\\x{4e00},\\x{9fa5}].
Return value
A value of the STRING type is returned. The following rules apply:
If pattern is an empty string, or if pattern contains no capturing groups and group_id is not specified, an error is returned.
NoteRun
SET odps.sql.bigquery.compatible=true;to enable BigQuery compatible mode. If pattern contains no groups and group_id is not specified, the function returns the substring that matches the entire pattern.If group_id is not of the BIGINT type or is less than 0, an error is returned.
If source, pattern, or group_id is NULL, NULL is returned.
Examples
Example 1: Extract a substring from the string
foothebarusing the patternfoo(.*?)(bar). The following command is an example:-- Returns the because group_id is not specified. SELECT REGEXP_EXTRACT('foothebar', 'foo(.*?)(bar)'); -- Returns foothebar because group_id is set to 0. SELECT REGEXP_EXTRACT('foothebar', 'foo(.*?)(bar)', 0);Example 2: Extract a substring from the string
8d99d8using the pattern8d(\\d+)d8. The following command is an example:-- Returns 99. When you submit a SQL statement for regular expression calculation on a MaxCompute client, use two backslashes (\\) as escape characters. SELECT REGEXP_EXTRACT('8d99d8', '8d(\\d+)d8');Example 3: Extract the Chinese characters and punctuation from
【Alibaba Cloud】aliyun. The following command is an example:-- Returns 【Alibaba Cloud】. SELECT REGEXP_EXTRACT('【Alibaba Cloud】aliyun', '([^\\x{00}-\\x{ff}]+)');Example 4: Extract the Chinese characters from
【Alibaba Cloud】aliyun. The following command is an example:-- Returns Alibaba Cloud. SELECT REGEXP_EXTRACT('【Alibaba Cloud】aliyun', '([\\x{4e00}-\\x{9fa5}]+)');Example 5: Extract the Chinese characters from
【Alibaba Cloud】aliyunAlibaba Cloud. You cannot use the REGEXP_EXTRACT function for this scenario. Instead, use the REGEXP_REPLACE function. The following command is an example:-- Returns 【Alibaba Cloud】Alibaba Cloud. SELECT REGEXP_REPLACE('【Alibaba Cloud】aliyunAlibaba Cloud','([\\x{00}-\\x{ff}])', '');Example 6: Use a pattern that contains no groups. The following commands are examples:
Scenario 1: If pattern contains no groups and group_id is not specified, an error is returned. In BigQuery compatible mode, the function returns the substring that matches the entire pattern.
-- An error is returned. FAILED: ODPS-0130071:[0,0] Semantic analysis exception - physical plan generation failed: SQL Runtime Unretryable Error: ODPS-0121065:Argument value out of range - Regex group count is 0, but the specified group index is 1 SELECT REGEXP_EXTRACT('foothebar', 'thebar'); -- Enable BigQuery compatible mode. SET odps.sql.bigquery.compatible=true; -- Returns thebar. SELECT REGEXP_EXTRACT('foothebar', 'thebar');Scenario 2: If pattern contains no groups and group_id is set to 0, the function returns the substring that matches the entire pattern.
-- Returns thebar. SELECT REGEXP_EXTRACT('foothebar', 'thebar',0);
Related functions
REGEXP_EXTRACT is a string function. For more information about other string functions, see String Functions Overview.