Home  >  Article  >  Database  >  Detailed explanation of the use of RLIKE operator in MySQL

Detailed explanation of the use of RLIKE operator in MySQL

藏色散人
藏色散人Original
2019-04-19 17:36:4719679browse

In MySQL, the RLIKE operator is used to determine whether a string matches a regular expression. It is a synonym for REGEXP_LIKE().

The result is 1 if the string matches the provided regular expression, 0 otherwise.

The syntax is like this:

expr RLIKE pat

where expr is the input string and pat is the regular expression of the test string.

Example

Here is an example of how to use this operator in a SELECT statement:

SELECT 'Tweet' REGEXP '^Tw.*t$';

Result:

+--------------------------+
| 'Tweet' REGEXP '^Tw.*t$' |
+--------------------------+
|                        1 |
+--------------------------+

In this case, a return value of 1 indicates that the input string matches the regular expression. In particular, we specify that the input string should start with Tw and end with t (this is because we started the pattern ^Tw and ended with t$). The . part specifies any character, and * specifies that it can be zero for any number of that (any) character. So .* means there can't be a character, one character or many characters, between start and end.

Here's what happens if we remove the *:

SELECT 'Tweet' REGEXP '^Tw.t$';

Result:

+-------------------------+
| 'Tweet' REGEXP '^Tw.t$' |
+-------------------------+
|                       0 |
+-------------------------+

Returning a result of 0 means no match. This is because . specifies only one instance of any character. Our input string contains two instances

SELECT 
  'Twet' REGEXP '^Tw.t$' AS 'Twet',
  'Twit' REGEXP '^Tw.t$' AS 'Twit',
  'Twt' REGEXP '^Tw.t$' AS 'Twt',
  'Tw.t' REGEXP '^Tw.t$' AS 'Tw.t';

Result:

+------+------+-----+------+
| Twet | Twit | Twt | Tw.t |
+------+------+-----+------+
|    1 |    1 |   0 |    1 |
+------+------+-----+------+

Related recommendations: "mysql tutorial"

The above is the detailed content of Detailed explanation of the use of RLIKE operator in MySQL. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn