sql的匹配和正则表达式
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的匹配和正则表达式的更多相关文章
- SQL模糊匹配之正则表达式
− 方括号[ ]:指定一个字符.字符串.匹配他们中的任意一个. − 示例1:查询用户名以J或者以M开头的用户信息 − SELECT user_name FROM ecs_ ...
- [转载]sql 盲注之正则表达式攻击
[转载]sql 盲注之正则表达式攻击 -----------------------------------------MYSQL 5+-------------------------------- ...
- JAVA基础之sql模糊匹配、外键以及jsp中include的用法
一.SQL模糊匹配 适用于对字符串进行模糊搜索 格式: 字段名 Like '%关键词%' % 表示这个位置可有任意个字符(没有也可以) %关键词% 只要包含关键词就算 ...
- SQL中常用模糊查询的四种匹配模式&&正则表达式
执行数据库查询时,有完整查询和模糊查询之分.一般模糊语句如下:SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式:1.%:表示任意0个或多个字 ...
- SQL Server中使用正则表达式
SQL Server 2005及以上版本支持用CLR语言(C# .NET.VB.NET)编写过程.触发器和函数,因此使得正则匹配,数据提取能够在SQL中灵活运用,大大提高了SQL处理字符串,文本等内容 ...
- sql模糊匹配
执行 数据库查询时,有完整查询和模糊查询之分. 一般模糊语句如下: SELECT 字段 FROM 表 WHERE 某字段 Like 条件 其中关于条件,SQL提供了四种匹配模式: 1,%:表示任意0个 ...
- 中文字符匹配js正则表达式
普遍使用的正则是[\u4e00-\u9fa5],但这个范围并不完整.例如: /[\u4e00-\u9fa5]/.test( '⻏' ) // 测试部首⻏,返回false 根据Unicode 5 ...
- sql模糊匹配中%、_的处理
防sql注入之模糊匹配中%._处理: StringBuilder sbSql = new StringBuilder(); sbSql.Append(@"SELECT * from tabl ...
- sql 盲注之正则表达式攻击
-----------------------------------------MYSQL 5+----------------------------------------- 我们都已经知道,在 ...
随机推荐
- [LeetCode] 213. House Robber II 打家劫舍 II
Note: This is an extension of House Robber. After robbing those houses on that street, the thief has ...
- GitLab - 安装并启动GitLab
1 - GitLab安装 1.1 信息确认 [Anliven@node102 ~]$ uname -a Linux node102 3.10.0-957.el7.x86_64 #1 SMP Thu N ...
- spring AOP注解实现
一.什么是AOP 引用一下维基百科的定义 面向切面的程序设计(Aspect-oriented programming,AOP,又译作面向方面的程序设计.剖面导向程序设计)是计算机科学中的一种程序设计思 ...
- [JAVA] maven 阿里云节点 settings.xml
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...
- 第I位是0/1
int a; scanf("%d",&a); ;i<;i++) { ;//从右往左第i位是x ,i==0,就是第一位 printf("%d ",x ...
- 自定义 Word 默认的 Normal.dotm 模板、更改 Word 默认字体、更改 Word 默认样式(16)
1. 引言 以Office 2016为例. 有没有遇见这样的问题: 每次新建一个 Word 空白文档打开后字体默认是等线,段落默认是单倍行距,默认标题也不是自己想要的样式,等一系列问题.每次打开都要调 ...
- CF1051D Bicolorings
题目描述 咳咳,懒得复制了上面是两张图:) 解题思路 这题是一道很好的题,感觉之前做过,一开始手推状态找规律,可以用状压但是没想到 借鉴了一下大佬的dp modify数组用以累加新增的状态数 dp数组 ...
- 44 容器(三)——ArrayList索引相关方法
方法都比较简单,这里列出来即可: add(index,ele) //忘制定下标插入元素 add(ele) addAll(Collection <C> c) 泛型必须与调用add的泛型保持一 ...
- 【HC89S003F4开发板】 10汇编指令
HC89S003F4开发板汇编指令 一.数据传递类指令 MOV.MOVC.MOVX 1.MOV,用于片内数据存储器中的数据传递指令中. 2.MOVC是与ROM之间的数据传送,而MOVX是与外部RAM数 ...
- docker自动化脚本
使用脚本从git上拉取项目并运行, 有些不足的地方 编写脚本 run.sh 如果用到redis和myslq,要先启动redis和mysql #!/bin/bash # author:qiao # 更新 ...