All Products
Search
Document Center

MaxCompute:Regular expressions

Last Updated:Oct 13, 2025

This topic describes the metacharacters, character groups, and escape characters that are supported by regular expressions in MaxCompute SQL.

Metacharacters

The following table lists the common metacharacters that are supported by MaxCompute SQL.

Metacharacter

Description

^

Matches the beginning of a string.

$

Matches the end of a string.

.

Matches any single character.

*

Matches the preceding character or character pattern zero or more times.

+

Matches the preceding character or character pattern one or more times.

?

  • Matches the preceding character or character pattern zero or one time.

  • Serves as a matching modifier. When this character follows any other limiting character (* , +, ?, {n}, {n,}, or {n,m}), the matching pattern is non-greedy. The non-greedy pattern matches as few characters as possible in the searched string, whereas the default greedy pattern matches as many characters as possible in the searched string.

A|B

Matches A or B.

(abc)*

Matches the abc sequence zero or more times.

{n} or {m,n}

The number of matches. {n} represents the exact number of matches, and {m,n} represents a range of match counts.

[ab]

Matches any character in the brackets.

[a-d]

Matches one of the following characters: a, b, c, and d.

[^ab]

^ indicates NOT. This expression matches any character that is not a or b.

[::]

For more information, see POSIX character groups later in this topic.

\

Escape character. For more information, see Escape character matching later in this topic.

\n

n is a digit ranging from 1 to 9 and indicates back reference.

\d

Digits.

\D

Non-digit characters.

Note
  • MaxCompute SQL supports Perl Compatible Regular Expressions (PCRE). For more information about regular expressions, see PCRE documentation.

  • If the matching result does not meet your expectations, see Escape character matching to evaluate whether escape characters are required.

Examples

General rule matching

  • Match the beginning and end of a string

    -- Match whether the beginning of the string aa123bb is a. true is returned.
    SELECT 'aa123bb' RLIKE '^a';
    
    -- Match the beginning of the string aa123bb with a and the end of the string aa123bb with b. true is returned.
    SELECT 'aa123bb' RLIKE '^a.*b$';
    
    -- Match whether the string footerbar contains foo and bar in sequence. true is returned.
    SELECT 'footerbar' RLIKE 'foo(.*?)(bar)';
    
    -- Match whether the string footerbar begins with foo and ends with bar. true is returned.
    SELECT 'footerbar' RLIKE '^foo(.*?)(bar)$';
  • Match any character

    -- Match whether the beginning of the string cc123bb is any character from a to d. true is returned.
    SELECT 'cc123bb' RLIKE '^[a-d]';
    
    -- Match whether the string 12abc34 begins with 12, ends with 34, and contains at least one character from a to d in the middle. true is returned.
    SELECT '12abc34' RLIKE '^12[a-d]+34$';

Escape character matching

The RLIKE operator supports the backslash \ as an escape character. Therefore, all \ characters in the pattern of a regular expression must be escaped twice.

  • Example 1

    The regular expression needs to match the string a+b, where + is a special character in regular expressions. Therefore, it must be expressed as an escape character. In the regular expression engine, it is expressed as a\+b. Because another layer of escaping is required, the expression that can match this string is a\\+b.

    SELECT 'a+b' RLIKE 'a\\+b';
    
    --The following result is returned:
    +------+
    | _c1  |
    +------+
    | true |
    +------+
  • Example 2

    To match the character \, in the regular expression engine \ is a special character, so it needs to be represented as \\, and because another layer of escaping is required, it is written as \\\\.

    Note

    In MaxCompute SQL, a\\b is written, but a\b is displayed in the output. This is because MaxCompute escapes the expression.

    SELECT 'a\\b', 'a\\b' RLIKE 'a\\\b';
    
    -- The following result is returned:
    +-----+------+
    | _c0 | _c1  |
    +-----+------+
    | a\b | false |
    +-----+------+
    
    SELECT 'a\\b', 'a\\b' RLIKE 'a\\\\b';
    
    -- The following result is returned:
    +-----+------+
    | _c0 | _c1  |
    +-----+------+
    | a\b | true |
    +-----+------+
  • Example 3

    If a string contains a tab character (TAB), the system has already stored \t as a single character when it reads these two characters. Therefore, it is also a regular character in the pattern of the regular expression.

    SELECT 'a\tb', 'a\tb' RLIKE 'a\tb';
    
    -- The following result is returned:
    +---------+------+
    | _c0     | _c1  |
    +---------+------+
    | a     b | true |
    +---------+------+
  • Example 4

    Match strings that contain digits \d, non-digit characters \D, and back references \n.

    --Match whether the beginning of the string 2025maxcompute is a digit. true is returned.
    SELECT '2025maxcompute' RLIKE '^\\\d';
    
    --Match whether the beginning of the string maxcompute2025test is a non-digit character. true is returned.
    SELECT 'maxcompute2025test' RLIKE '^\\\D';
    
    --Match whether the string alibaba-cloud-MC2025-test contains the combination of MC and digits. true is returned.
    SELECT 'alibaba-cloud-MC2025-test' RLIKE 'MC\\\d';
    
    -- Match whether the string alibaba-cloud-MC2025-test contains the combination of MC and four digits. true is returned.
    SELECT 'alibaba-cloud-MC2025-test' RLIKE 'MC\\\d{4}-';
    
    -- Match whether the string contains abcdefdef. true is returned.
    -- abc is capturing group 1, def is capturing group 2, \2 represents repeated matching of the second capturing group, which needs to be escaped again as \\\2. That is, abcdefdef.
    SELECT 'mmabcdefdefgg' RLIKE '(abc)(def)\\\2';

POSIX character groups

Character group

Description

Valid value

[[:alnum:]]

Letters and digits

[a-zA-Z0-9]

[[:alpha:]]

Letters

[a-zA-Z]

[[:ascii:]]

ASCII characters

[\x00-\x7F]

[[:blank:]]

Spaces and tab characters

[ \t]

[[:cntrl:]]

Control characters

[\x00-\x1F\x7F]

[[:digit:]]

Digits

[0-9]

[[:graph:]]

Characters other than whitespace characters

[\x21-\x7E]

[[:lower:]]

Lowercase letters

[a-z]

[[:print:]]

[:graph:] and whitespace characters

[\x20-\x7E]

[[:punct:]]

Punctuations

[][!"#$%&'()*+,./:;<=>? @\^_`{|}~-]

[[:space:]]

Whitespace characters

[ \t\r\n\v\f]

[[:upper:]]

Uppercase letters

[A-Z]

[[:xdigit:]]

Hexadecimal characters

[A-Fa-f0-9]

Chinese character groups

Character group

Valid value

Double-byte characters such as Chinese characters

[^\\x{00}-\\x{ff}]

Chinese characters

[\\x{4e00}-\\x{9fa5}]

Chinese punctuations

Chinese punctuations do not have a unified encoding range. You can search for Chinese punctuation Unicode in a search engine and then use operators to exclude them one by one.

Example

For example, the regular expression for the period () is [\\x{3002}].

SELECT * FROM VALUES ('Hello.'),('nihao!') t(d) WHERE d RLIKE '[\\x{3002}]';

-- The following result is returned:
+------------+
| d          |
+------------+
| Hello.     |
+------------+
Note

To match a single quotation mark, you can use unicode0027, which corresponds to the regular expression [\\x{0027}].