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

Detailed explanation of the use of RLIKE operator in MySQL

藏色散人
Release: 2019-04-19 17:36:47
Original
19718 people have browsed it

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
Copy after login

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$';
Copy after login

Result:

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

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$';
Copy after login

Result:

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

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';
Copy after login

Result:

+------+------+-----+------+
| Twet | Twit | Twt | Tw.t |
+------+------+-----+------+
|    1 |    1 |   0 |    1 |
+------+------+-----+------+
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template