欢迎您访问 最编程 本站为您分享编程语言代码,编程技术文章!
您现在的位置是: 首页

HIVE 正则表达式函数(like、rlike、regexp、regexp_replace、regexp_extract)

最编程 2024-04-01 19:39:29
...

Java正则:

"." 任意单个字符
"*" 匹配前面的字符0次或多次
"+" 匹配前面的字符1次或多次
"?" 匹配前面的字符0次或1次
"\d" 等于 [0-9],使用的时候写成'\d'
"\D" 等于 [^0-9],使用的时候写成'\D'

hive> select 'does' rlike 'do(es)?';
OK
true

hive> select '\\';
OK
\

hive> select '2314' rlike '\\d+';
OK
true

REGEXP

语法1: A REGEXP B
语法2: REGEXP(A, B)
操作类型: strings
返回类型: boolean或null
描述: 功能与RLIKE相同

hive> select 'football' regexp 'ba';
OK
true


hive> select 'football' regexp '^footba';
OK
true


hive> select regexp('football', 'ba');
OK
true


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

hive> select regexp_replace('h234ney', '\\d+', 'o');
OK
honey

REGEXP_REPLACE

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

hive> select regexp_replace('h234ney', '\\d+', 'o');
OK
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);
OK
honeymoon
 
 
hive> select regexp_extract('honeymoon', 'hon(.*?)(moon)', 1);
OK
ey
 
 
hive> select regexp_extract('honeymoon', 'hon(.*?)(moon)', 2);
OK
moon