在hive的regex中匹配多个结果

ruyhziif  于 2021-04-03  发布在  Hive
关注(0)|答案(1)|浏览(1033)

当我运行
select regexp_extract("hosts: 192.168.1.1 192.168.1.2 host",'((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)',0);
我得到了 "192.168.1.1"。
但我想要的是192.168.1.1,192.168.1.2["192.168.1.1", "192.168.1.2"]
我应该做什么,改变注册或创建一个udf?

o2gm4chl

o2gm4chl1#

拆分字符串,爆炸,检查每个部分是否与regexp匹配,收集匹配部分的数组,如果需要从数组中获取字符串,使用concat_ws()来连接数组。

with your_data as(
select stack(1, 'hosts: 192.168.1.1 192.168.1.2 host' ) as hosts
)

select collect_set(case when part rlike '((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)' 
                        then part 
                  else null end )
from your_data d
     lateral view explode(split(hosts, ' +')) s as part;

result:

["192.168.1.1","192.168.1.2"]

相关问题