赞
踩
一、like
使用场景:
1、%:百分号代表任意个字符
(1)查询name以“张”开头的数据:
如查询test表中如下数据:
select * from test where name like ‘张%’;
(2)查询name以“三”年结尾的数据:
select * from test where name like ‘%三’;
(3)查询name中包含“测试”的数据:
select * from test where name like ‘%测试%’;
如果要对like进行否定,前面加上not即可,
select * from test where name not like ‘%测试%’;
2、_:下划线代表一个字符
(1)查询name由两个字构成,第二个字是“斯”的数据:
select * from test where name like ‘_斯’;
(2) 查询姓名由两个字构成,第二个字不是“琪”:
select * from test where name not like ‘_琪’;
二、REGEXP和RLIKE
语法1: A REGEXP B
语法2: REGEXP(A, B)
操作类型: strings
返回类型: boolean或null
说明:如果字符串A或者字符串B为NULL,则返回NULL;如果字符串A符合JAVA正则表达式B的正则语法,则为TRUE;否则为FALSE。
注: RLIKE与REGEXP功能相同
hive> select 'football' regexp 'ba';
true
hive> select regexp('football', 'ba');
true
hive> select 'football' regexp '^footba';
true
hive> select 'football,basketball,volleyball' regexp 'foot|basket';
true
hive> select 'football,volleyball' regexp 'football|basket';
true
三、REGEXP_REPLACE
语法: regexp_replace(string A, string B, string C)
操作类型: strings
返回值: string
说明: 将字符串A中的符合java正则表达式B的部分替换为C。
hive> select regexp_replace('h234ney', '\\d+', 'o');
honey
四、REGEXP_EXTRACT
语法: regexp_extract(string A, string pattern, int index)
返回值: string
说明:将字符串A按照pattern正则表达式的规则拆分,返回index指定的字符,index从1开始计。
hive> select regexp_extract('honeymoon', 'hon(.*?)(moon)', 0);
honeymoon
hive> select regexp_extract('honeymoon', 'hon(.*?)(moon)', 1);
ey
hive> select regexp_extract('honeymoon', 'hon(.*?)(moon)', 2);
moon
附:
Java正则:
“|” 或
“&” 与
“.” 任意单个字符
“*” 匹配前面的字符0次或多次
“+” 匹配前面的字符1次或多次
“?” 匹配前面的字符0次或1次
“\d” 等于 [0-9],使用的时候写成’\d’
“\D” 等于 [^0-9],使用的时候写成’\D’
hive> select 'does' rlike 'do(es)?';
true
hive> select '\\';
\
hive> select '2314' rlike '\\d+';
true
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。