服务器之家:专注于服务器技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - Mysql - MySQL中使用正则表达式详情

MySQL中使用正则表达式详情

2021-12-07 18:14李子捌 Mysql

这篇文章主要给大家分享的是MySQL中使用正则表达式详情,MySQL中支持正则表达式匹配,在复杂的过滤条件中,可以考虑使用正则表达式。使用正则表达式需要掌握一些正则表达式的语法和指令,下面来看看文章的详细内容介绍吧,

MySQL中使用正则表达式详情

1、简介

mysql中支持正则表达式匹配,在复杂的过滤条件中,可以考虑使用正则表达式。使用正则表达式需要掌握一些正则表达式的语法和指令,小捌推荐一个学习地址和在线工具,在学习mysql中使用正则表达式之前,去了解一下正则表达式的语法和指令。

正则表达式学习网址:

www.runoob.com/regexp/rege…

正则表达式在线测试:

c.runoob.com/front-end/8…

值得注意的是,mysql支持的正则表达式仅仅是正则表达式众多实现的一个子集,在使用正则表达式之前,建议先测试一下。 测试的时候不一定要先建立表、插入数据,可以直接使用select省略form子句,以简便的方式处理表达式,

比如如下方式:

?
1
2
3
4
5
6
mysql> select '我爱你中国' regexp '我爱你';
+------------------------------+
| '我爱你中国' regexp '我爱你' |
+------------------------------+
|                            1 |
+------------------------------+
?
1
2
3
4
5
6
mysql> select '12306' regexp '[:digit:]';
+----------------------------+
| '12306' regexp '[:digit:]' |
+----------------------------+
|                          1 |
+----------------------------+

2、准备一张product表

首先准备一张product表,ddl和表数据如下所示,可以直接复制使用。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
10
mysql> 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的产品

语句:

?
1
mysql> 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的产品

语句:

?
1
mysql> 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关键字即可。

语句:

?
1
mysql> select * from product where product_name regexp binary 'huawei';

结果:

?
1
empty set (0.00 sec)

由于product表中并没有包含小写huawei的产品,所以返回结果为empty set

2.3 正则表达式与like的区别

相信有些小伙伴发现上面实现的功能,其实用like也能实现。很多场景下我们使用like来对字符串进行匹配,但是这些场景往往非常简单,而正则表达式是一个非常强大的文本检索过滤工具,它的所能实现的功能比like操作符强大太多啦。总之like能做的正则表达式都能做,正则表示能做的like基本上做不了(要么非常棘手)。

比如如下需求,使用正则表达式可以轻松实现,但是like操作符却不知道怎么实现了。

需求:

查询产品表中,产品名称中v至少出现一次产品信息

语句:

?
1
mysql> select * from product where product_name regexp 'v+';

结果:

?
1
2
3
4
5
+----+---------------+---------+
| id | product_name  | price   |
+----+---------------+---------+
|  5 | vivo x70 pro+ | 5999.00 |
+----+---------------+---------+

注意:正则表达式重复元字符的匹配都是整个连续出现。下面给出这几个重复元字符,感觉有些小伙伴会理解错。

重复元字符

 

元字符 说明
* 0个或多个匹配,效果与{0,}一致
+ 1个或多个匹配,效果与{1,}一致
1个或0匹配,效果与{0,1}一致
{n} 等于n个匹配数目
{n,} 大于等于n个匹配
{n,m} 大于等于n小于等于m个,m<255


原文链接:https://juejin.cn/post/7031706858003890189

延伸 · 阅读

精彩推荐