Regular expressions

Updated at:

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

Metacharacters

MaxCompute SQL supports the following commonly used metacharacters, as shown in the table below.

Metacharacter

Description

^

Matches the beginning of a line.

$

Matches the end of a line.

.

Matches any single character.

*

Matches the preceding character or pattern zero or more times.

+

Matches the preceding character or pattern one or more times.

?

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

  • When placed after a quantifier (*, +, ?, {n}, {n,}, or {n,m}), switches that quantifier to non-greedy mode. Non-greedy mode matches as few characters as possible; the default greedy mode matches as many as possible.

A|B

Matches A or B.

(abc)*

Matches the sequence abc zero or more times.

{n} or {m,n}

The number of matches. {n} matches exactly n times. {m,n} matches between m and n times.

[ab]

Matches any one of the characters in brackets.

[a-d]

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

[^ab]

The caret (^) indicates negation. Matches any character other than a or b.

[::]

For more information, see POSIX character groups below.

\

Escape character. For more information, see Escape character matching below.

\n

A backreference, where n is a digit in the range 1~9.

\d

Matches a digit.

\D

Matches any non-digit character.

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

  • If the matching result is unexpected, see Escape character matching to determine whether escaping is required.

Examples

General pattern matching

  • Matching the beginning and end of a string

    -- Check whether the string aa123bb starts with a. Returns true.
    SELECT 'aa123bb' RLIKE '^a';
    
    -- Check whether the string aa123bb starts with a and ends with b. Returns true.
    SELECT 'aa123bb' RLIKE '^a.*b$';
    
    -- Check whether the string footerbar contains foo followed by bar. Returns true.
    SELECT 'footerbar' RLIKE 'foo(.*?)(bar)';
    
    -- Check whether the string footerbar starts with foo and ends with bar. Returns true.
    SELECT 'footerbar' RLIKE '^foo(.*?)(bar)$';
  • Matching any character

    -- Check whether the string cc123bb starts with any character in the range a to d. Returns true.
    SELECT 'cc123bb' RLIKE '^[a-d]';
    
    -- Check whether the string 12abc34 starts with 12, ends with 34, and contains at least one character in the range a to d in between. Returns true.
    SELECT '12abc34' RLIKE '^12[a-d]+34$';

Escape character matching

The RLIKE operator uses a backslash (\) as the escape character. Therefore, any backslash (\) that appears in a regular expression pattern must be escaped twice.

  • Example 1

    To match the string a+b, where + is a special regex character, it must be escaped. In the regex engine, this is expressed as a\+b. Because an additional layer of escaping is required, the expression that matches the string is a\\+b.

    SELECT 'a+b' RLIKE 'a\\+b';
    
    -- Results:
    +------+
    | _c1  |
    +------+
    | true |
    +------+
  • Example 2

    To match the backslash character \, which is a special character in the regex engine and must be expressed as \\, an additional layer of escaping requires it to be written as \\\\.

    Note

    In MaxCompute SQL, a\\b is written in the expression, but a\b is displayed in the output, because MaxCompute applies escaping to the expression.

    SELECT 'a\\b', 'a\\b' RLIKE 'a\\\b';
    
    -- Results:
    +-----+------+
    | _c0 | _c1  |
    +-----+------+
    | a\b | false |
    +-----+------+
    
    SELECT 'a\\b', 'a\\b' RLIKE 'a\\\\b';
    
    -- Results:
    +-----+------+
    | _c0 | _c1  |
    +-----+------+
    | a\b | true |
    +-----+------+
  • Example 3

    If the string contains a tab character (TAB), the system stores \t as a single character when reading it. Therefore, in the regex pattern, it is treated as a regular character.

    SELECT 'a\tb', 'a\tb' RLIKE 'a\tb';
    
    -- Results:
    +---------+------+
    | _c0     | _c1  |
    +---------+------+
    | a     b | true |
    +---------+------+
  • Example 4

    Match strings that contain digits (\d), non-digits (\D), and backreferences (\n).

    -- Check whether the string 2025maxcompute starts with a digit character. Returns true.
    SELECT '2025maxcompute' RLIKE '^\\\d';
    
    -- Check whether the string maxcompute2025test starts with a non-digit character. Returns true.
    SELECT 'maxcompute2025test' RLIKE '^\\\D';
    
    -- Check whether the string alibaba-cloud-MC2025-test contains MC followed by digits. Returns true.
    SELECT 'alibaba-cloud-MC2025-test' RLIKE 'MC\\\d';
    
    -- Check whether the string alibaba-cloud-MC2025-test contains MC followed by exactly 4 digits. Returns true.
    SELECT 'alibaba-cloud-MC2025-test' RLIKE 'MC\\\d{4}-';
    
    -- Check whether the string contains abcdefdef. Returns true.
    -- abc is capture group 1, def is capture group 2, and \2 represents a backreference to the second capture group, which must be escaped again as \\\2. This matches abcdefdef.
    SELECT 'mmabcdefdefgg' RLIKE '(abc)(def)\\\2';

POSIX character groups

Character group

Description

Range

[[:alnum:]]

Letters and digits

[a-zA-Z0-9]

[[:alpha:]]

Letters

[a-zA-Z]

[[:ascii:]]

ASCII characters

[\x00-\x7F]

[[:blank:]]

Space 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:]]

Punctuation characters

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

[[:space:]]

Whitespace characters

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

[[:upper:]]

Uppercase letters

[A-Z]

[[:xdigit:]]

Hexadecimal characters

[A-Fa-f0-9]

Chinese character groups

Chinese character subgroup

Range

Double-byte characters, including Chinese characters

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

Chinese characters

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

Chinese punctuation

Chinese punctuation marks do not have a unified encoding range. Search for Unicode codes for Chinese punctuation marks in a search engine, then combine them with operators to exclude the characters individually.

Examples

For example, the Chinese period (。) corresponds to the regular expression [\\x{3002}].

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

-- Results:
+------------+
| d          |
+------------+
| 你好。        |
+------------+
Note

To match a single quotation mark, use unicode0027. The corresponding regular expression is [\\x{0027}].