2026年04月17日 10:10:13 来源:安徽思成仪器技术有限公司 >> 进入该公司展台 阅读量:0
MySQL正则表达式匹配查询 一、正则表达式介绍
在过滤的时候允许使用匹配、比较和通配符寻找数据。对于基本的过滤,这些可能就足够了。但是随着过滤条件复杂性的增加,where子句本身的复杂性也有必要增加。
这里简单介绍一下使用正则表达式匹配搜索。所有种类的程序设计语言、文本编辑器、操作系统都支持正则表达式。很多程序员都将正则表达式作为自己的技能。熟练使用正则表达式,可以帮助我们减少很多的麻烦。
正则表达式用正则表达式语言来建立,正则表达式语言是用来完成过滤、匹配类工作的一种特殊语言。与其他语言一样,它用于自己的特殊的语法和指令。
二、使用正则表达式
正则表达式的作用是匹配文本,将一个模式与一个文本串进行比较,根据自定义的模式,过滤出你需要的数据。MySQL的正则表达式只是正则表达式的一个子集。
测试数据
mysql> select * from regexp_test;+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 |+------+----------+7 rows in set (0.00 sec)1.基本字符匹配
使用like匹配name为xiao的用户,需要借助通配符%
mysql> select * from regexp_test where name like 'xiao%';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua |+------+----------+3 rows in set (0.01 sec)mysql> explain select * from regexp_test where name like 'xiao%';使用regexp匹配name为xiao的用户,无需借助任何通配符
regexp在列值内进行匹配,如果被匹配的文本在列值中出现,regexp将会找到他,相应的行将被返回。
regexp正则表达式匹配不区分大小写
mysql> select * from regexp_test where name regexp 'xiao';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua |+------+----------+3 rows in set (0.00 sec)
2.OR匹配
使用or匹配多个符合条件的数据
mysql> select * from regexp_test where id = 1 or id = 2;+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)使用regexp匹配多个符合条件的数据
使用regexp的|功能类似于在select中使用or
mysql> select * from regexp_test where id regexp '1|2';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)3.模糊匹配
使用or模糊匹配
mysql> select * from regexp_test where id = 1 or id = 2 or id = 8;+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)使用|匹配其中符合条件的
mysql> select * from regexp_test where id regexp '1|2|8';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)使用[]匹配符合条件
mysql> select * from regexp_test where id regexp '[128]';+------+----------+| id | name |
+------+----------+| 1 | xiaoming || 2 | xiaohong |+------+----------+2 rows in set (0.00 sec)4.匹配范围
[123456789]即为匹配到123456789这个集合
[1-9]即为匹配到123456789这个集合
[a-z]匹配任意字母
mysql> select * from regexp_test where name regexp '[a-z]';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 |+------+----------+7 rows in set (0.00 sec)mysql> select * from regexp_test where id regexp '[0-9]';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 |+------+----------+7 rows in set (0.00 sec)5.匹配特殊字符
.和-在正则表达式中是特殊字符,需要使用两个双引号//转义,例如//-表示查找-,例如//.表示查找.
原字符说明
//f
换页
//n
换行
//r
回车
/
制表
//v
纵向制表
至于为什么要有两个反斜杠,MySQL要求需要两个,一个是MySQL自身需要,一个正则表达式需要

## 模拟插入一条带.的数据mysql> insert into regexp_test values(8,'z.y');## 因为.匹配任意字符,所以会把所有数据都匹配到mysql> select * from regexp_test where name regexp '.';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 || 8 | z.y |+------+----------+8 rows in set (0.00 sec)## 当使用两个反斜杠转义后,查询出的结果和我们的期望一致mysql> select * from regexp_test where name regexp '//.';+------+------+| id | name |+------+------+| 8 | z.y |+------+------+1 row in set (0.00 sec)6. 匹配字符类
存在找出经常使用使用数字、所有字母字符或所有数字字母字符的匹配。为方便使用,可以采取预定义的字符集,称为字符类。
类说明
[:alnum:]
任意字符和数字,同[a-zA-Z0-9]
[:alpha:]
任意字符,同[a-zA-Z]
[:blank:]
空格和制表,同[/]
[:cntrl:]
ASCAII控制字符mysql 正则匹配中文,ASCAII 0 到31和127
[:digit:]
任意数字,同[0-9]
[:graph:]
与[:print:]相同,但不包括空格
[:lower:]
任意小写字母,同[a-z]
[:print:]
任意可打印字符
[:punct:]
既不在[:alnum:]又不在[:cntrl:]中的任意字符
[:space:]
包括空格在内的任意空白字符,同[//f//n//r///v]
[:upper:]

任意大写字母,同[A-Z]
[:xdigit:]
任意十六进制数字,同[a-fA-F0-9]
7.匹配多个实例
目前为止使用的所有正则表达式都尝试匹配单次出现。如果存在一个匹配,该行被检索出来,如果不存在mysql 正则匹配中文,检索不出任何行。但有时需要对匹配的数目进行更强的控制。例如,你可能需要寻找所有的数,不管数中包含多少数字,或者你可能想寻找一个单词并且能够适应一个跟随的字符,等。
元字符说明
*
0个或多个匹配
+
1个或多个匹配
?
?匹配它前面的任何字符的0次活1次出现
{n}
指定数目的匹配
{n,}
不少于指定数目的匹配
{n,m}
匹配数目的范围(m不超过255)
例一:匹配到满足xiaoh和xiao的数据
mysql> select * from regexp_test where name regexp 'xiaoh?';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua |+------+----------+3 rows in set (0.01 sec)例二 :仔细观察{4}和{1}匹配到数据的不同点
## 匹配4个连续小写字母mysql> select * from regexp_test where name regexp '[a-z]{4}';+------+----------+| id | name |+------+----------+| 1 | xiaoming || 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 |+------+----------+7 rows in set (0.00 sec)## 匹配1个连续小写字母mysql> select * from regexp_test where name regexp '[a-z]{1}';+------+----------+| id | name |+------+----------+| 1 | xiaoming |
| 2 | xiaohong || 3 | xiaohua || 4 | zhangsan || 5 | lisi || 6 | liwu || 7 | liliu9 || 8 | z.y |+------+----------+8 rows in set (0.00 sec)8.定位符
以上介绍中都是匹配到一个字符串中任意位置的文本。为了匹配特定位置的文本,可以参考使用定位符:
元字符说明
^
文本开始
$
文本结尾
[[::]]
词的结尾
notes:特别需要注意的是,当在`[]`内的时候代表的是否定该集合,当在[]外的时候代表的是文本开始
案例一:匹配以数字结尾
mysql> select * from regexp_test where name regexp '[a-z][0-9]$';+------+--------+| id | name |+------+--------+| 7 | liliu9 |+------+--------+1 row in set (0.01 sec)案例二:匹配以数字开头
mysql> select * from regexp_test where name regexp '^[0-9][a-z]';+------+-------+| id | name |+------+-------+| 9 | 1zbc || 10 | 1qwr2 |+------+-------+2 rows in set (0.00 sec)案例三:匹配以数字开头,以数字结尾
mysql> select * from regexp_test where name regexp '^[0-9][a-z]*[0-9]$';+------+-------+| id | name |+------+-------+| 10 | 1qwr2 |+------+-------+1 row in set (0.00 sec)