mysql 笔记三 (联系正则表达式)

来源:互联网 发布:时间自动校准软件 编辑:程序博客网 时间:2024/06/07 03:07
//正则表达式
关键字 regexp
//匹配姓名中包含‘123’的人
select * from user where name regexp '123'
//通配符  .
select * from user where name regexp '.2';
查询姓名的任意字符后面跟2的人


//区别
select * from user where name like 'gao'
select * from user where name like '%gao%'
select * from user where name regexp 'gao'
结果第二个与第三个相同,与第一个不同

//正则表达式不区分大小写的
如果想区分,前面得加 binary
select * from user where name regexp binary 'Gao';


//与or进行匹配
select * from user where name regexp 'gao|liu';

//匹配几个字符中的一个
select * from user where name regexp '[gl]ao';
只能找出 gao 或者 lao
'[^gl]'
则匹配gao 和lao以外的

[0123456789] ==[1-9]


匹配特殊字符要用转义
.  \\.
|  \\|
[   \\[
]   \\]
\   \\\

\\f  换页
\\n   换行
\\r  回车
\\t  制表
\\v  纵向制表


正则表达式的函数

[:alnum:]  任意字母和数字[a-zA-Z0-9]
[:alpha:]  任意字符 [a-zA-z]
[:digit:]  任意数字 [0-9]
[:upper:]  任意大写字母[A-Z]
[:lower:]  任意小写字母[a-z]



正则表达式中特殊符号的含义
*  0个或多个匹配
+  1个或多个匹配
? 0个或1个匹配
{n} 指定数目的匹配
{n,} 不少于指定数目的匹配
{n,m} 匹配数目的范围
^ 文本的开始
$ 文本的结束