4.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.
SQL pattern matching enables you to use “_
” to match any single character and “%
” to match an arbitrary number of characters (including zero characters). In MySQL, SQL patterns are case-insensitive by default. Some examples are shown here. You do not use =
or <>
when you use SQL patterns; use the LIKE
orNOT LIKE
comparison operators instead.
To find names beginning with “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
”:
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
”:
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. When you test for a match for this type of pattern, use theREGEXP
and NOT REGEXP
operators (or RLIKE
and NOT RLIKE
, which are synonyms).
The following list describes some characteristics of extended regular expressions:
- “
.
” matches any single character. - A character class “
[...]
” matches any character within the brackets. For example, “[abc]
” matches “a
”, “b
”, or “c
”. To name a range of characters, use a dash. “[a-z]
” matches any letter, whereas “[0-9]
” matches any digit. - “
*
” matches zero or more instances of the thing preceding it. For example, “x*
” matches any number of “x
” characters, “[0-9]*
” matches any number of digits, and “.*
” matches any number of anything. - A
REGEXP
pattern match succeeds if the pattern matches anywhere in the value being tested. (This differs from aLIKE
pattern match, which succeeds only if the pattern matches the entire value.) - To anchor a pattern so that it must match the beginning or end of the value being tested, use “
^
” at the beginning or “$
” at the end of the pattern.
To demonstrate how extended regular expressions work, the LIKE
queries shown previously are rewritten here to use REGEXP
.
To find names beginning with “b
”, use “^
” to match the beginning of the name:
mysql> SELECT * FROM pet WHERE name REGEXP '^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 |
+--------+--------+---------+------+------------+------------+
If you really want to force a REGEXP
comparison to be case sensitive, use the BINARY
keyword to make one of the strings a binary string. This query matches only lowercase “b
” at the beginning of a name:
mysql> SELECT * FROM pet WHERE name REGEXP BINARY '^b';
To find names ending with “fy
”, use “$
” to match the end of the name:
mysql> SELECT * FROM pet WHERE name REGEXP '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:
mysql> SELECT * FROM pet WHERE name REGEXP '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 like it would be if you used an SQL pattern.
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 name REGEXP '^.....$';+-------+--------+---------+------+------------+-------+
| 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:
mysql> SELECT * FROM pet WHERE name REGEXP '^.{5}$';+-------+--------+---------+------+------------+-------+
| name | owner | species | sex | birth | death |
+-------+--------+---------+------+------------+-------+
| Claws | Gwen | cat | m | 1994-03-17 | NULL |
| Buffy | Harold | dog | f | 1989-05-13 | NULL |
+-------+--------+---------+------+------------+-------+
Section 13.5.2, “Regular Expressions”, provides more information about the syntax for regular expressions.
- 【深度学习】图片风格转换应用程序:使用CoreML创建Prisma
- 压力测试中存在的问题
- 这或许是对小白最友好的python入门了吧——18,用while循环处理列表
- 如何在js中将统计代码图标隐藏
- 警惕IT黑洞
- 这或许是对小白最友好的python入门了吧——17,while循环
- 泛型介绍(接上一篇,具体的事例随后呈上)
- PHP高级编程之守护进程
- 看到他我一下子就悟了-- 泛型(2)
- ExtJS4中设置tabpanel的tab高度问题
- Oracle写错误与文件离线
- 看到他我一下子就悟了-- 泛型(1)
- EXTJS4 Grid Filter 插件的使用 与后台数据解析------Extjs 查询筛选功能的实现
- [Java]读取文件方法大全
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- ESP8266(一)| 基于OneNet的温度采集显示系统
- 隐秘的 MySQL 类型转换
- 对Jenkinsfile语法说不,开源项目Jenkins Json Build挺你
- Volatile概述
- SparkStreaming和Kafka基于Direct Approach如何管理offset
- Spark在处理数据的时候,会将数据都加载到内存再做处理吗?
- 美团点评2020年测试工程师笔试题
- Pytest之重运行及生成测试报告
- 一条SQL的奇妙旅行
- 表格的融合
- 解放双手,使用批处理达到账号密码之类再也用不着手动输入!
- 教你在几分钟内构建一个Python包
- 撸了一个 Feign 增强包
- YCbCr444转YCbCr422
- fpga实现YCbCr422转YCbCr444