All Products
Search
Document Center

MaxCompute:REGEXP_EXTRACT_ALL

Last Updated:Jul 17, 2023

Finds all substrings that match the pattern of a regular expression in a string and returns the substrings as an array.

Syntax

array<T> regexp_extract_all(string <source>, string <pattern>[,bigint <group_id>])

Parameters

  • source: required. A value of the STRING type. This parameter specifies the string that you want to analyze.

  • pattern: required. A value of the STRING type. This parameter specifies the pattern that you want substrings to match. This parameter can be a constant of the STRING type or a regular expression. For more information about regular expressions, see Regular expressions.

  • group_id: optional. A value of the BIGINT type. This parameter specifies the ID of the group that is used to match the pattern. The value of this parameter must be greater than or equal to 0. If you do not specify this parameter, the group with an ID of 1 is used to match the pattern. If you set this parameter to 0, all groups are used to match the pattern.

Return value

A value of the ARRAY type is returned. If you specify group_id, an array that consists of all matching results for the group specified by group_id is returned. If you do not specify group_id, an array that consists of all matching results for the group with an ID of 1 is returned.

Examples

  • If you do not specify group_id, an array that consists of all matching results for the group with an ID of 1 is returned. Sample statement:

    SELECT regexp_extract_all('100-200, 300-400', '(\\d+)-(\\d+)');

    The following result is returned:

    +------------+
    | _c0        |
    +------------+
    | [100,300] |
    +------------+
  • If you set group_id to 2, an array that consists of all matching results for the group with an ID of 2 is returned. Sample statement:

    SELECT regexp_extract_all('100-200, 300-400', '(\\d+)-(\\d+)',2);

    The following result is returned:

    +------------+
    | _c0        |
    +------------+
    | [200,400] |
    +------------+

Related functions

REGEXP_EXTRACT_ALL is a string function. For more information about functions related to string searches and conversion, see String functions.