当前位置:   article > 正文

Hive-sql常用正则函数_hivesql 正则

hivesql 正则

一、like
使用场景:

  • 模糊查询然后进行剔除测试数据,例:客户名称 not 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

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

三、REGEXP_REPLACE
语法: regexp_replace(string A, string B, string C)
操作类型: strings
返回值: string
说明: 将字符串A中的符合java正则表达式B的部分替换为C。

hive> select regexp_replace('h234ney', '\\d+', 'o');
honey
  • 1
  • 2

四、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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8

附:
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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/396664?site
推荐阅读
相关标签
  

闽ICP备14008679号