RLIKE Function
Overview
The RLIKE function is used to check if a string (str) matches a given regular expression (regex). If it matches, it returns true; otherwise, it returns false. This function uses the re2 regular expression engine for matching.
Syntax
Parameters
- str (string): The string to be matched.
- regex (string): Regular expression. Please note that since the SQL parser will escape string constants, you need to use double backslashes when specifying the regular expression with constants (e.g., '\\abc$').
Return Value
Returns a boolean value indicating whether str matches regex.
Example Usage
- Check if the string "100-500" matches the regular expression
\d+-\d+(numbers plus a hyphen plus numbers):
Results:
- Use a function to check if the string "100-500" matches the regular expression
\d+-\d+:
Results:
- Check if the string "\abc" matches the regular expression
\\\\abc$(a string that starts and ends with "abc", ignoring the leading and trailing backslashes):
Results:
- Check if the string "abc" does not match the regular expression
\\d+$(a string that starts and ends with a digit):
Results:
- Use the "not" keyword to check if the string "123" does not match the regular expression
\d+(a string containing digits):
Results:
Through the above examples, you can better understand the usage and functionality of the RLIKE function. In practical applications, you can use different regular expressions to match and validate strings as needed.
