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

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

服务器之家 - 数据库 - Mysql - MySql比较运算符正则式匹配REGEXP的详细使用详解

MySql比较运算符正则式匹配REGEXP的详细使用详解

2021-02-26 17:34姚鑫国 Mysql

这篇文章主要介绍了MySql比较运算符正则式匹配REGEXP的详细使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

 一、初始化数据

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
drop table if exists `test_01`;
create table `test_01` (
 `id` int(0) not null,
 `stu` varchar(255) character set utf8mb4 collate utf8mb4_0900_ai_ci null default null comment '学号',
 `user` varchar(255) character set utf8mb4 collate utf8mb4_0900_ai_ci null default null comment '用户',
 `km` varchar(255) character set utf8mb4 collate utf8mb4_0900_ai_ci null default null comment '科目',
 `fs` varchar(255) character set utf8mb4 collate utf8mb4_0900_ai_ci null default null comment '分数',
 `time` datetime(0) null default null comment '时间',
 primary key (`id`) using btree
) engine = innodb character set = utf8mb4 collate = utf8mb4_0900_ai_ci row_format = dynamic;
 
insert into `test_01` values (1, 'x0219001', '小三', '语文', '98', '2020-08-06 15:51:21');
insert into `test_01` values (2, 'x0219001', '小三', '数学', '90', '2020-07-01 15:51:25');
insert into `test_01` values (3, 'x0219001', '小三', '英语', '77', '2020-06-01 15:51:28');
insert into `test_01` values (4, 'x0219002', '小二', '语文', '98', '2020-08-06 15:51:21');

1、基本字符匹配

匹配字段中包含 ‘x' 的学号。不区分大小写

?
1
select * from test_01 where stu regexp 'x';

MySql比较运算符正则式匹配REGEXP的详细使用详解

2、'.' 表示匹配任意一个字符

需要匹配多个字符就多打几个点

?
1
2
select * from test_01 where stu regexp '.9001';
select * from test_01 where stu regexp '.02..0';

MySql比较运算符正则式匹配REGEXP的详细使用详解

3、' | '表示为搜索两个串之一

?
1
select * from test_01 where user regexp '二|四';

MySql比较运算符正则式匹配REGEXP的详细使用详解

4、 ‘[ ]' 匹配任何单一字符

?
1
select * from test_01 where stu regexp '0[23]';

在这里 [23] 相当于[2|3],一个[]匹配一个字符。

MySql比较运算符正则式匹配REGEXP的详细使用详解

匹配范围
[0123456789] 或 [0-9] 将匹配数字0到9
[a-z] 匹配任意字母符号

5、匹配特殊字符

1.\ 转义字符

即转义:正则表达式内具有特殊意义的所有字符都必须以这种方式转义。

 

元字符 说明
\\- 表示查找 -
\\. 表示查找 .

 

2.\ 也用来引用元字符

 

元字符 说明
\f 换页
\n 换行
\r 回车
\t 制表
\v 纵向制表

 

3.匹配多实例

 

元字符 说明
* 0个或多个匹配
+ 1个或多个匹配(等于 {1, })
? 0个或1个匹配(等于 {0, 1})
{n} 指定数目的匹配
{n, } 不少于指定数目的匹配
{n ,m} 匹配数目的范围(m不超过255)

 

4.匹配字符类

 

代码 解释
[:a;num:] 任意字母和数字(同 [a-za-z0-9])
[:alpha:] 任意字符(同 [a-za-z])
[:blank:] 空格和制表(同 [\t])
[:cntrl:] ascii控制字符(ascii 0到31和127)
[:digit:] 任意数字(同[0-9])
[:graph:] 与["print:] 相同,但不包括空格
[:lower:] 任意小写字线(同 [a-z])
[:print:] 任意可打印字符
[:punct:] 既不在 [:alnum:] 又不在 [:cntrl:] 中的任意字符
[space:] 包括空格在内的任意空白字符(同 [\f\n\t\r\v])
[:upper:] 任意大小字母(同 [a-z])
[:xdigit:] 任意十六进制数字(同 [a-fa-f0-9])

 

到此这篇关于mysql比较运算符正则式匹配regexp的详细使用详解的文章就介绍到这了,更多相关mysql 正则式匹配regexp内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/weixin_44325655/article/details/107856587

延伸 · 阅读

精彩推荐