各位用户为了找寻关于MySQL中使用正则表达式详情的资料费劲了很多周折。这里教程网为您整理了关于MySQL中使用正则表达式详情的相关资料,仅供查阅,以下为您介绍关于MySQL中使用正则表达式详情的详细内容
目录
1、简介 2、准备一张product表 2.1 语句顺序 2.2 如何区分大小写 2.3 正则表达式与like的区别
1、简介
mysql
中支持正则表达式匹配,在复杂的过滤条件中,可以考虑使用正则表达式。使用正则表达式需要掌握一些正则表达式的语法和指令,小捌推荐一个学习地址和在线工具,在学习mysql
中使用正则表达式之前,去了解一下正则表达式的语法和指令。
正则表达式学习网址:
www.runoob.com/regexp/rege…
正则表达式在线测试:
c.runoob.com/front-end/8…
值得注意的是,mysql
支持的正则表达式仅仅是正则表达式众多实现的一个子集,在使用正则表达式之前,建议先测试一下。 测试的时候不一定要先建立表、插入数据,可以直接使用select省略form子句,以简便的方式处理表达式,
比如如下方式:
? 1 2 3 4 5 6mysql>
select
'我爱你中国'
regexp
'我爱你'
;
+
------------------------------+
|
'我爱你中国'
regexp
'我爱你'
|
+
------------------------------+
| 1 |
+
------------------------------+
?
1
2
3
4
5
6
mysql>
select
'12306'
regexp
'[:digit:]'
;
+
----------------------------+
|
'12306'
regexp
'[:digit:]'
|
+
----------------------------+
| 1 |
+
----------------------------+
2、准备一张product表
首先准备一张product
表,ddl和表数据如下所示,可以直接复制使用。
set
names utf8mb4;
set
foreign_key_checks = 0;
-- ----------------------------
-- table structure for product
-- ----------------------------
drop
table
if exists `product`;
create
table
`product` (
`id`
int
(11)
not
null
auto_increment comment
'主键'
,
`product_name`
varchar
(255)
character
set
utf8
collate
utf8_general_ci
not
null
comment
'产品名称'
,
`price`
decimal
(10, 2) unsigned
not
null
comment
'产品价格'
,
primary
key
(`id`) using btree
) engine = innodb
character
set
= utf8
collate
= utf8_general_ci row_format =
dynamic
;
-- ----------------------------
-- records of product
-- ----------------------------
insert
into
`product`
values
(1,
'apple iphone 13 (a2634)'
, 6799.00);
insert
into
`product`
values
(2,
'huawei p50 pro'
, 6488.00);
insert
into
`product`
values
(3,
'mix4'
, 4999.00);
insert
into
`product`
values
(4,
'oppo find x3'
, 3999.00);
insert
into
`product`
values
(5,
'vivo x70 pro+'
, 5999.00);
set
foreign_key_checks = 1;
初始数据如下所示:
? 1 2 3 4 5 6 7 8 9 10mysql>
select
*
from
product;
+
----+-------------------------+---------+
| id | product_name | price |
+
----+-------------------------+---------+
| 1 | apple iphone 13 (a2634) | 6799.00 |
| 2 | huawei p50 pro | 6488.00 |
| 3 | mix4 | 4999.00 |
| 4 | oppo find x3 | 3999.00 |
| 5 | vivo x70 pro+ | 5999.00 |
+
----+-------------------------+---------+
2.1 语句顺序
正则表达式的作用是文本匹配,使用一个正则表达式与一个文本内容进行比较,可以校验文本是否符合正则表达式阐述的规则。在mysql
中,正则表达式用在where
子句中,可以对select
查询的数据进行过滤。
select * from table_name where regexp
'你的正则表达式' order by cloumn_name
;
需求:
查询产品表中,产品名称中包含3的产品
语句:
? 1mysql>
select
*
from
product
where
product_name regexp
'3'
;
结果:
? 1 2 3 4 5 6+
----+-------------------------+---------+
| id | product_name | price |
+
----+-------------------------+---------+
| 1 | apple iphone 13 (a2634) | 6799.00 |
| 4 | oppo find x3 | 3999.00 |
+
----+-------------------------+---------+
2.2 如何区分大小写
mysql
使用正则表达式默认不区分大小写,但是大部分情况下我们需要明确英文的大小写进行匹配,这个时候我们可以使用binary
关键字。
需求:
查询产品表中,产品名称包含huawei的产品
语句:
? 1mysql>
select
*
from
product
where
product_name regexp
'huawei'
;
结果:
? 1 2 3 4 5+
----+----------------+---------+
| id | product_name | price |
+
----+----------------+---------+
| 2 | huawei p50 pro | 6488.00 |
+
----+----------------+---------+
此时查询结果默认不区分大小写,所以可以直接查询出来,如果我们希望查询区分大小写,此时只需要在regexp
后面加上binary
关键字即可。
语句:
? 1mysql>
select
*
from
product
where
product_name regexp
binary
'huawei'
;
结果:
? 1empty
set
(0.00 sec)
由于product
表中并没有包含小写huawei
的产品,所以返回结果为empty set
2.3 正则表达式与like的区别
相信有些小伙伴发现上面实现的功能,其实用like也能实现。很多场景下我们使用like来对字符串进行匹配,但是这些场景往往非常简单,而正则表达式是一个非常强大的文本检索过滤工具,它的所能实现的功能比like操作符强大太多啦。总之like能做的正则表达式都能做,正则表示能做的like基本上做不了(要么非常棘手)。
比如如下需求,使用正则表达式可以轻松实现,但是like操作符却不知道怎么实现了。
需求:
查询产品表中,产品名称中v至少出现一次产品信息
语句:
? 1mysql>
select
*
from
product
where
product_name regexp
'v+'
;
结果:
? 1 2 3 4 5+
----+---------------+---------+
| id | product_name | price |
+
----+---------------+---------+
| 5 | vivo x70 pro+ | 5999.00 |
+
----+---------------+---------+
注意:正则表达式重复元字符的匹配都是整个连续出现。下面给出这几个重复元字符,感觉有些小伙伴会理解错。
重复元字符
原文链接:https://juejin.cn/post/7031706858003890189