果然学习好是有道理的,学习Mysql与正则表达式笔记

正则表达式是用来匹配文本的特殊的字符集合,将一个正则表达式与文本串进行比较,Mysql中用where子句提供支持,正则表达式关键字:regexp
1、使用‘|’匹配两个串中的一个

 

2、使用‘[]’匹配几个字符中的一个

 

3、使用‘[^]’不匹配几个字符串中的任意一个,表示否定

 

4、对于特殊字符的匹配
需要在待匹配的特殊字符前面加上‘\\’进行转义。

 

5、Mysql支持的正则表达式字符集

正则表达式是用来匹配文本的特殊的字符集合,如果想从一个文本文件中提取电话号码,可以用正则表达式完成
作用:匹配文本,将一个正则表达式与一个文本串进行比较
格式:where子句中使用regexp 关键字,后面接正则表达式字符

与Like的区别:
where可以使用like (not like)和regexp (not regexp)来匹配特定的内容
1、like匹配整列数据
2、regexp 可以匹配列中任意位置的文本,更灵活更强大


完全匹配
select prod_name from products where prod_name regexp '1000';

 

等同于like语句
select prod_name from products where prod_name like '%1000%';

| 两个串中间的一个,效果等同于or or是完全匹配,不能模糊匹配

或者,不能放在like后面,放在regexp后面
select prod_name from products where prod_name regexp '1000|2000';

[] 匹配同个字符中的一个,与in相似,in是完全匹配

匹配1或2或3
select prod_name from products where prod_name regexp '[123] ton';
匹配包含12 ton数据
select prod_name from products where prod_name regexp '[123][123] ton';

[^ ] 不匹配几个字符中的任意一个

不匹配括号中任意一个字符的结果,除了123,其它都会被查询出来
select prod_name from products where prod_name regexp '[^123] ton';

[1-9]匹配一个范围1到9的数字
[a-z]包含a到z的任意一个字符

匹配1至5的任意数字 1 ton ,2 ton,3 ton,4 ton,5 ton
select prod_name from products where prod_name regexp '[1-5] ton';

\\对于特殊字符的匹配需要转义

匹配点.
select vend_name from vendors where vend_name regexp '\\.';



  正则表达式字符集

 

[:alnum:]任意字母和数字[a-zA-Z0-9]
[:alpha:]任意字符[a-zA-Z]
[:upper:]任意大写字母[A-Z]
[:lower:]任意小写字母[a-z]
[:digit:]任意数字[0-9]
[:xdigit]任意十六进制字[a-fA-F0-9]
[:blank:]空格和制表[\\t]
[:space:]包括空格在内的任意空白字符[\\f\\n\\r\\t\\v]
[cntrl:] ascii控制字符0-37和127
[:graph:]与[:print:]相同,不包括空格
[:print:]任意可打印字符
[:punct:]排除[:alnum]和[:cntrl:]的任意字符

*  0或多个匹配 与Like后面的%相似
+  1个或多个匹配{1,} 针对这个位置前面的字符匹配
?  0个或1个匹配 {0,1}
n   指定数目的匹配
{n,}  不少于指定数目的匹配 出现n次以上
{n,m}  匹配数目的范围,m不超过255 出现n次以上少于m次
^   文本的开始
$   文本的结尾
[[:<:]]   单词的开始[[:<:]] a app
[[:>:]]   单词的结尾


匹配连在一起的4位数字
select prod_name from products where prod_name regexp '[[:digit:]]{4}';



  练习

 

查找prod_id包含anv的数据
select * from products where prod_id regexp 'anv';

如果需要区分大小写,binary关键字
select * from products where binary prod_id regexp 'ANV';

查找prod_id包含anv或者包含f的数据
select * from products where prod_id regexp 'anv|f';

包含3,c,e的数据
select * from products where prod_id regexp '[3ce]';

select * from products where prod_id regexp '[^3ce]'; 没有定位哪一个字符

查找f开头不包含3,c,e的数据
select * from products where prod_id regexp 'f[^3ce]';

查找prod_desc含有.开头的数据
select * from products where prod_desc regexp '^\\.';

前面是任意字符与数字,点后面有9或没有都可以被查找出来
select * from products where prod_price regexp '[[:alnum:]]\\.9*';

9后面出现一个或多个都能被查出来
select * from products where prod_price regexp '[[:alnum:]]\\.9+';

点后面出0个或1个都可以被查询出来
select * from products where prod_price regexp '[[:alnum:]]\\.9?';

?是针对.的限制,有没有.都可以被查出来
select * from products where prod_price regexp '[[:alnum:]]\\.?';

查找以k结尾的数据
select * from products where prod_price regexp 'k$';

查找d开头的数据
select * from products where prod_price regexp '^d';

查找单个d的数据
select * from products where prod_price regexp '^d$';

查找以d开头以d结尾的数据
select * from products where prod_price regexp '^d.*d$';

查找以p单词开头的数据
select * from products where prod_desc regexp '[[:<:]]'p;

查找以f结尾单词的数据
select * from products where prod_desc regexp 'f[[>:]]';

 

posted @ 2020-05-07 10:14  测试幼儿生  阅读(209)  评论(0编辑  收藏  举报