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

str [not] rlike regex rlike(str, regex)

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

  1. Check if the string "100-500" matches the regular expression \d+-\d+ (numbers plus a hyphen plus numbers):

SELECT '100-500' RLIKE '\d+-\d+';

Results:

true

  1. Use a function to check if the string "100-500" matches the regular expression \d+-\d+:

SELECT RLIKE('100-500', '\d+-\d+');

Results:

true

  1. Check if the string "\abc" matches the regular expression \\\\abc$ (a string that starts and ends with "abc", ignoring the leading and trailing backslashes):

SELECT RLIKE('\\abc', '\\\\abc$');

Results:

true

  1. Check if the string "abc" does not match the regular expression \\d+$ (a string that starts and ends with a digit):

SELECT 'abc' RLIKE '\\d+$';

Results:

false

  1. Use the "not" keyword to check if the string "123" does not match the regular expression \d+ (a string containing digits):

SELECT '123' NOT RLIKE '\d+';

Results:

false

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.