基本字符匹配
select prod_name
from products
where prod_name regexp '.000'
order by prod_name;
分析
这里使用了正则表达式 .000,是正则表达式语言中一个特殊的字符。它表示匹配任意一个字符,因此,1000和2000都匹配且返回。
注意:
LIKE和REGEXP之间有一个重要的区别
LIKE匹配整个列
REGEXP匹配字符
区分大小写:
select prod_name
from products
where prod_name regexp BINARY 'JetPack.000'
order by prod_name;
进行OR匹配
select prod_name
from products
where prod_name regexp '1000 | 2000'
order by prod_name;
分析
两个或者两个以上的用 ‘ 1000 | 2000 | 3000 | 4000’
匹配几个字符之一
select prod_name
from products
where prod_name regexp '[123] Ton'
order by prod_name;
分析
[123] Ton 的意思是匹配1或2或3,因此,1 Ton和2 Ton都匹配且返回
匹配范围
select prod_name
from products
where prod_name regexp '[1-5] Ton'
order by prod_name;
分析
匹配1-5
匹配特殊字符
select prod_name
from products
where prod_name regexp '\\.'
order by prod_name;
分析
\. 匹配,检索出一行,转义匹配 .
\f 换页
\n 换行
\r 回车
\t 制表
\v 纵向制表
匹配\ \
MySQL要用\:MySQL自己解释一个,正则表达式解释一个
匹配字符类
匹配多个实例
select prod_name
from products
where prod_name regexp '\\([0-9] sticks?\\)'
order by prod_name;
分析
\(匹配)
[0-9]匹配任意数字
s后?使s可选
\ )匹配)
匹配连在一起的4位数字:
select prod_name
from products
where prod_name regexp '[[:digit:]]{4}'
order by prod_name;select prod_name
from products
where prod_name regexp '[0-9][0-9][0-9][0-9]'
order by prod_name;
定位符
select prod_name
from products
where prod_name regexp '\^ [0-9\\.]'
order by prod_name;
分析
^的双重用途:在集合中用来否定该集合,用来指串的开始处