1. 匹配:like 关键字

#假设存在表 my_test_copy
select * from my_test_copy;

则使用like关键词匹配:注意下划线 '_'和百分号 '%'

# 下划线'_'匹配任意单个字符
# 百分号'%'匹配任意数目的字符
select * from my_test_copy where name_people like '_满_'; #有值
select * from my_test_copy where name_people like '%满%'; #有值,百分号可以匹配任意数目的字符 select * from my_test_copy where name_people like '%楼'; #有值,百分号可以匹配任意数目的字符
select * from my_test_copy where name_people like '_楼'; #为空,因为下划线只能代表一个字符
select * from my_test_copy where name_people like '_满楼'; #有值

2. 正则表达式:'rlike' 和 'regexp'同义,用于标识正则表达式模式

(1)'rlike' 和 'regexp'

select 'abc' regexp 'ab'; # regexp左操作数是要匹配的字符串,右操作数是匹配模式
select * from my_test_copy where name_people regexp '花';
select * from my_test_copy where name_people rlike '花';

(2)直接匹配

select 'abc' regexp 'b'; #返回1
select * from my_test_copy where name_people regexp '满'; #有值

(3)'^'符号,匹配开头,从字符串开始进行匹配

select * from my_test_copy where name_adress regexp '^天坛'; #为空,用于查询以....开头的字符串,而没有以'天坛'开头的字符串
select * from my_test_copy where name_adress regexp '^北'; #有值,用于查询以....开头的字符串

(4)'$'符号,匹配结尾

select * from my_test_copy where name_adress regexp '北$'; #为空,用于查询以....结尾的字符串
select * from my_test_copy where name_adress regexp '京$'; #有值

(5)点号 '.' 符号,匹配任意一个字符

select * from my_test_copy where name_adress regexp '北.'; #有值
select * from my_test_copy where name_adress regexp '.京$'; #有值

(6)'+'符号,+前面的模式至少出现1次或以上

select * from my_test_copy where name_adress regexp 'beijing+'; #有值
select * from my_test_copy where name_adress regexp '京+'; #有值

(7)'*'符号,*前面的模式出现0次或以上

select * from my_test_copy where name_adress regexp '北京*'; #有值,'北'出现,且'京'接着'北'出现0次以上
select * from my_test_copy where name_adress regexp '人*'; #有值,出现0次

(8)'?'符号,'?'前面的模式出现0次或1次

select 'abc' regexp '^(ab)?c$'; #有值
select 'aaabc' regexp '^a*b?c$'; #有值
select 'ababc' regexp '^(ab)+c$'; #有值
select * from my_test_copy where name_adress regexp '人?'; #有值,出现0次
select * from my_test_copy where name_adress regexp '京?'; #有值,出现1次
select * from my_test_copy where name_adress regexp '(山庄)?'; #有值,出现0次或1次(山庄)
select * from my_test_copy where name_adress regexp '山庄?'; #有值,出现了'山'和1次'庄'

(9)'()'符号,表示一个整体

select '' regexp '(34)1'; #返回0:表示没找到

select 'ac' regexp '^(ab)?c$'; #为0,表示未找到
select 'ababc' regexp '^(ab)?c$'; # 结果为0,'?'表示出现0次或1次 开头'(ab)',而这个句子中出现了2次'ab',不符合条件,可以写成 select 'ababc' regexp '^(ab){1,3}c$'; # {1,3}表示(ab)出现的次数在1-3次都可以

(10)'[]'符号,表示对其中的任意一个字符进行匹配,且仅能匹配一个字符

select 'heo' regexp '^h[abcde]o$'; #为1
select 'hello' regexp '^h[abcde]o$'; #为0,,虽然能匹配'e',但是两个'l'没有匹配
select 'hello' regexp '^h[abcdehijklmn]o$'; #为0,,虽然能匹配'e'和'l',但是'[]'每次只能匹配其中一个字符
select * from my_test_copy where name_adress regexp '万梅[山庄阵]'; #有值,匹配到'万梅山'

(11)'[]'中可以使用'-'表示区间,表示该区间的任意一个字符,my_test是另一个表

select 'abcde' regexp '^a[a-k]';
select 'abcde' regexp '[a-k]'; #查找是否存在字母
select * from my_test where phone_number regexp '[0-9]'; #有值,匹配到数字
select * from my_test where phone_number regexp '12[a-z]'; #为空

(12)若'[]'需要匹配']',则']'必须紧跟在'['之后

select 'aacc' regexp 'a[]a-z]'; #有值
select 'aa]cc' regexp 'a[]]'; #有值
select 'aa]cc' regexp 'a[bcc]dd]'; #为0,因为']'不是紧跟着'['
select 'aa]cc' regexp 'a[]bcac]'; #为1
select 'aa]cc' regexp 'a[bc]]'; #为0,']'放在末尾也不行

(13)若'[]'需要匹配'-',则'-'需要放在'[]'两端,放在中间可能会报错:[Err] 3697 - The regular expression contains an [x-y] character range where x comes after y.

select 'aaa-ccc-ddd' regexp 'a[-]'; #有值
select 'aaa-ccc-ddd' regexp 'a[-cdf]'; #有值,匹配到'a-'
select 'aaa-ccc-ddd' regexp 'a[d-a]'; #[Err] 3697 - The regular expression contains an [x-y] character range where x comes after y.

(14)[^]表示不含'[]'中的任意字符

#例如:不含数字
select 'aaabbb' regexp '[^0-9]'; #为1 select 'aaabbb' regexp '[^0-9]$'; #为1,不以数字结尾
select 'aaabbb233' regexp '[^0-9]$'; #为0 select 'aaabbb' regexp '^[^0-9]'; #为1,不以数字开头
select '23aaabbb' regexp '^[^0-9]'; #为0 select * from my_test_copy where name_adress regexp '[^北京]'; #表示不能只有'北京'
select * from my_test_copy where name_adress regexp '^[^北]'; #表示开头不含'北'
select * from my_test_copy where name_adress regexp '万梅[^山]'; #表示不含'万梅山'

(15)'|' 匹配分隔的任意一个字符

select 'abc' regexp 'b|a|c';
select * from my_test_copy where name_adress regexp '北京|南京'; #表示含有'北京'或'南京'的

(16)'{t}' 匹配前面的字符t次

select 'ababc' regexp '^(ab){2}c$'; #为1
select 'ababc' regexp '^ab{2}c$'; #为0,因为'b'没有出现2次
select 'abbbc' regexp '^ab{3}c$'; #为1

(17)'{t,s}'匹配前面的字符t-s次均可,t<=s

select 'abbbc' regexp '^ab{2,4}c$'; #为1,因为'b'出现3次
select 'abc' regexp '^ab{2,4}c$'; #为0,因为'b'出现1次,不在2-4之间 select 'abbbbbbc' regexp '^ab{1,}c$'; #为1,{1,}缺省,表示出现1次及以上

(18){t,s}中不能出现空格,否则报错:[Err] 3692 - Incorrect description of a {min,max} interval.

select 'aoe' regexp '^ao{0 , 1}c$'; # [Err] 3692 - Incorrect description of a {min,max} interval.

#欢迎交流

参考:

https://www.cnblogs.com/liuwei6/p/7279542.html

https://blog.csdn.net/a1311010193/article/details/101388446

https://www.runoob.com/mysql/mysql-regexp.html

sql的匹配和正则表达式的更多相关文章

  1. SQL模糊匹配之正则表达式

    −      方括号[ ]:指定一个字符.字符串.匹配他们中的任意一个. −      示例1:查询用户名以J或者以M开头的用户信息 −      SELECT user_name FROM ecs_ ...

  2. [转载]sql 盲注之正则表达式攻击

    [转载]sql 盲注之正则表达式攻击 -----------------------------------------MYSQL 5+-------------------------------- ...

  3. JAVA基础之sql模糊匹配、外键以及jsp中include的用法

    一.SQL模糊匹配 适用于对字符串进行模糊搜索 格式:   字段名 Like '%关键词%'      %          表示这个位置可有任意个字符(没有也可以) %关键词%  只要包含关键词就算 ...

  4. SQL中常用模糊查询的四种匹配模式&&正则表达式

    执行数据库查询时,有完整查询和模糊查询之分.一般模糊语句如下:SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式:1.%:表示任意0个或多个字 ...

  5. SQL Server中使用正则表达式

    SQL Server 2005及以上版本支持用CLR语言(C# .NET.VB.NET)编写过程.触发器和函数,因此使得正则匹配,数据提取能够在SQL中灵活运用,大大提高了SQL处理字符串,文本等内容 ...

  6. sql模糊匹配

    执行 数据库查询时,有完整查询和模糊查询之分. 一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,%:表示任意0个 ...

  7. 中文字符匹配js正则表达式

    普遍使用的正则是[\u4e00-\u9fa5],但这个范围并不完整.例如:  /[\u4e00-\u9fa5]/.test( '⻏' ) // 测试部首⻏,返回false    根据Unicode 5 ...

  8. sql模糊匹配中%、_的处理

    防sql注入之模糊匹配中%._处理: StringBuilder sbSql = new StringBuilder(); sbSql.Append(@"SELECT * from tabl ...

  9. sql 盲注之正则表达式攻击

    -----------------------------------------MYSQL 5+----------------------------------------- 我们都已经知道,在 ...

随机推荐

  1. [Design Patterns] 02. Structural Patterns - Facade Pattern

    前言 参考资源 史上最全设计模式导学目录(完整版) 只把常用的五星的掌握即可. 外观模式-Facade Pattern[学习难度:★☆☆☆☆,使用频率:★★★★★] 深入浅出外观模式(一):外观模式概 ...

  2. [LeetCode] 229. Majority Element II 多数元素 II

    Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times. Note: The a ...

  3. svn服务安装教程

    https://www.cnblogs.com/yankyblogs/p/7282752.html

  4. 从GitLab上创建分支本地拉取项目和提交项目详解

    很多公司前端项目都是部署在GitLab上的,今天我要分享的就是如何从GitLab上创建新的分支并在本地拉取和提交项目 一.在GitLab上面创建自己新的分支 首先你得注册一个账号,登陆之后进入项目Pr ...

  5. 手撕面试官系列(二):开源框架面试题Spring+SpringMVC+MyBatis

    文章首发于今日头条:https://www.toutiao.com/i6712324863006081549/ 前言 跳槽时时刻刻都在发生,但是我建议大家跳槽之前,先想清楚为什么要跳槽.切不可跟风,看 ...

  6. Java开发笔记(一百三十八)JavaFX的箱子

    前面介绍了JavaFX标签控件的用法,其中提到Label文本支持中文字体,那么它到底支持哪些中文字体呢?自然要看当前的操作系统都安装了哪些字体才行,对于中文的Windows系统,默认安装了黑体“Sim ...

  7. web页面元素定位

    所有web网页中有8种元素定位方式 靠单一的特征找元素:6种(id,class_name,tag_name,name,link_text(2))组合各种特征和关系来找元素:2种(xpath,css) ...

  8. SQL——函数

    演示c_grade表 一.AVG() AVG()函数用于返回数值列的平均值 例: SELECT AVG(score) FROM c_grade; 运行结果: 通过运行结果可以看到,score字段为Nu ...

  9. Linux 打包和压缩

    常用的打包压缩方式 windows常用rar mac常用zip linux常用tar.gz 打包/解包 tar是linux中最常用的备份工具,此命令可以把一系列的文件打包到一个大文件中,也可以把一个打 ...

  10. Jenkins安装maven integration plugin失败解决方法

    最近装了一个jenkins准备搞一个自动化测试的持续集成,但是在安装maven integration这个插件时报错,试了几次都是失败! 错误原因如下: javadoc安装失败: java.io.IO ...