RLIKE character matching
RLIKE is an operator provided by MaxCompute SQL based on the PCRE (Perl Compatible Regular Expressions) specification. It supports more precise and complex pattern matching or replacement in SQL statements. This topic describes the metacharacters, POSIX character groups, Chinese character groups, escape characters, and related examples supported by RLIKE.
Metacharacters
The RLIKE operator supports a range of metacharacters. The following table lists the commonly used metacharacters:
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. |
|
|
A|B | Matches A or B. |
(abc)* | Matches the sequence abc zero or more 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. |
| Matches a digit. |
| Matches any non-digit character. |
For more information about using regular expressions, see the 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 asa\+b. Because an additional layer of escaping is required, the expression that matches the string isa\\+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\\\\.NoteIn MaxCompute SQL,
a\\bis written in the expression, buta\bis 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
\tas 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';
Character groups
POSIX character groups
Character group | Description | Range |
| Letters and digits |
|
| Letters |
|
[[:ascii:]] | ASCII characters | [\x00-\x7F] |
[[:blank:]] | Space and tab characters | [ \t] |
[[:cntrl:]] | Control characters | [\x00-\x1F\x7F] |
| Digits |
|
[[:graph:]] | Characters other than whitespace characters | [\x21-\x7E] |
| Lowercase letters |
|
[[:print:]] | [:graph:] and whitespace characters | [\x20-\x7E] |
[[:punct:]] | Punctuation characters | [][!”#$%&’()*+,./:;<=>? @\^_`{|}~-] |
| Whitespace characters |
|
| Uppercase letters |
|
[[:xdigit:]] | Hexadecimal characters | [A-Fa-f0-9] |
MaxCompute does not support \f or \v. For information about supported escape characters, see Escape characters.
Chinese character groups
Chinese character group | Range |
Double-byte characters (e.g., Chinese characters) |
|
Chinese characters |
|
Chinese punctuation | Chinese punctuation marks do not have a unified encoding range. Search for Unicode codes for Chinese punctuation marks, 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 |
+------------+
| 你好。 |
+------------+To match a single quotation mark, use Unicode code point 0027. The corresponding regular expression is [\\x{0027}].