3.3.4.7 Pattern Matching模式匹配

MySQL provides standard SQL pattern matching as well as a form of pattern matching based on extended regular expressions similar to those used by Unix utilities such as vi, grep, and sed.MySQL提供了标准的SQL模式匹配,以及一种基于扩展正则表达式的模式匹配形式,类似于Unix实用程序(如vigrepsed)所使用的正则表达式。

SQL pattern matching enables you to use _ to match any single character and % to match an arbitrary number of characters (including zero characters). SQL模式匹配允许您使用_匹配任何单个字符,使用%匹配任意数量的字符(包括零个字符)。In MySQL, SQL patterns are case-insensitive by default. 在MySQL中,默认情况下SQL模式不区分大小写。Some examples are shown here. 这里显示了一些示例。Do not use = or <> when you use SQL patterns. 当您使用SQL模式时,不要使用=<>Use the LIKE or NOT LIKE comparison operators instead.请改用LIKENOT LIKE比较运算符。

To find names beginning with b:要查找以b开头的名称,请执行以下操作:

mysql> SELECT * FROM pet WHERE name LIKE 'b%';
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1989-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+

To find names ending with fy:要查找以fy结尾的名称,请执行以下操作:

mysql> SELECT * FROM pet WHERE name LIKE '%fy';
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+

To find names containing a w:要查找包含w的名称,请执行以下操作:

mysql> SELECT * FROM pet WHERE name LIKE '%w%';
+----------+-------+---------+------+------------+------------+
| name     | owner | species | sex  | birth      | death      |
+----------+-------+---------+------+------------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
+----------+-------+---------+------+------------+------------+

To find names containing exactly five characters, use five instances of the _ pattern character:要查找正好包含五个字符的名称,请使用_模式字符的五个实例:

mysql> SELECT * FROM pet WHERE name LIKE '_____';
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

The other type of pattern matching provided by MySQL uses extended regular expressions. MySQL提供的另一种模式匹配使用扩展正则表达式。When you test for a match for this type of pattern, use the REGEXP_LIKE() function (or the REGEXP or RLIKE operators, which are synonyms for REGEXP_LIKE()).测试此类型模式的匹配时,请使用REGEXP_LIKE()函数(或REGEXPRLIKE运算符,它们是REGEXP_LIKE()的同义词)。

The following list describes some characteristics of extended regular expressions:下表介绍了扩展正则表达式的一些特性:

To demonstrate how extended regular expressions work, the LIKE queries shown previously are rewritten here to use REGEXP_LIKE().为了演示扩展正则表达式是如何工作的,这里重写了前面显示的LIKE查询,以使用REGEXP_LIKE()

To find names beginning with b, use ^ to match the beginning of the name:要查找以b开头的名称,请使用^匹配名称的开头:

mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, '^b');
+--------+--------+---------+------+------------+------------+
| name   | owner  | species | sex  | birth      | death      |
+--------+--------+---------+------+------------+------------+
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL       |
| Bowser | Diane  | dog     | m    | 1979-08-31 | 1995-07-29 |
+--------+--------+---------+------+------------+------------+

To force a regular expression comparison to be case-sensitive, use a case-sensitive collation, or use the BINARY keyword to make one of the strings a binary string, or specify the c match-control character. 要强制正则表达式比较区分大小写,请使用区分大小写的排序规则,或使用BINARY关键字将其中一个字符串设置为二进制字符串,或指定c匹配控制字符。Each of these queries matches only lowercase b at the beginning of a name:每个查询只匹配名称开头的小写b

SELECT * FROM pet WHERE REGEXP_LIKE(name, '^b' COLLATE utf8mb4_0900_as_cs);
SELECT * FROM pet WHERE REGEXP_LIKE(name, BINARY '^b');
SELECT * FROM pet WHERE REGEXP_LIKE(name, '^b', 'c');

To find names ending with fy, use $ to match the end of the name:要查找以fy结尾的名称,请使用$匹配名称的结尾:

mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, 'fy$');
+--------+--------+---------+------+------------+-------+
| name   | owner  | species | sex  | birth      | death |
+--------+--------+---------+------+------------+-------+
| Fluffy | Harold | cat     | f    | 1993-02-04 | NULL  |
| Buffy  | Harold | dog     | f    | 1989-05-13 | NULL  |
+--------+--------+---------+------+------------+-------+

To find names containing a w, use this query:要查找包含w的名称,请使用以下查询:

mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, 'w');
+----------+-------+---------+------+------------+------------+
| name     | owner | species | sex  | birth      | death      |
+----------+-------+---------+------+------------+------------+
| Claws    | Gwen  | cat     | m    | 1994-03-17 | NULL       |
| Bowser   | Diane | dog     | m    | 1989-08-31 | 1995-07-29 |
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL       |
+----------+-------+---------+------+------------+------------+

Because a regular expression pattern matches if it occurs anywhere in the value, it is not necessary in the previous query to put a wildcard on either side of the pattern to get it to match the entire value as would be true with an SQL pattern.因为正则表达式模式如果出现在值中的任何位置就会匹配,所以在前面的查询中不必在模式的任一侧放置通配符,以使其与整个值匹配,就像SQL模式一样。

To find names containing exactly five characters, use ^ and $ to match the beginning and end of the name, and five instances of . in between:若要查找正好包含五个字符的名称,请使用^$匹配名称的开头和结尾以及其间的五个.实例:

mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, '^.....$');
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

You could also write the previous query using the {n} (repeat-n-times) operator:您还可以使用{n}(重复n次)操作符编写前面的查询:

mysql> SELECT * FROM pet WHERE REGEXP_LIKE(name, '^.{5}$');
+-------+--------+---------+------+------------+-------+
| name  | owner  | species | sex  | birth      | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen   | cat     | m    | 1994-03-17 | NULL  |
| Buffy | Harold | dog     | f    | 1989-05-13 | NULL  |
+-------+--------+---------+------+------------+-------+

For more information about the syntax for regular expressions, see Section 12.8.2, “Regular Expressions”.有关正则表达式语法的更多信息,请参阅第12.8.2节,“正则表达式”