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

吾爱主题 阅读:116 2024-04-01 23:50:43 评论:0

 一、初始化数据

?
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' ;

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

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

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

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

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

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

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

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

匹配范围
[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

可以去百度分享获取分享代码输入这里。
声明

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

【腾讯云】云服务器产品特惠热卖中
搜索
标签列表
    关注我们

    了解等多精彩内容