sparksql 正则匹配总结
这里对sql常用的一些正则匹配作一些匹配,都是来源别人博客,此处稍作整理和总结。mark一下
1、sql中有like 和 rlike,具体区别
like:
%:匹配零个及多个任意字符
_:与任意单字符匹配
[]:匹配一个范围
[^]:排除一个范围
ESCAPE 关键字定义转义符 WHERE ColumnA LIKE '%5/%%' ESCAPE '/'
like不是正则,而是通配符
rlike:
rlike是正则,正则的写法与java一样。'\'需要使用'\\',例如'\w'需要使用'\\w'
A rlike '\\d+' 匹配一个或多个数字, not A rlike '\\d+' 匹配非数字
直接在条件里面写正则表达式
2、hive sql 中 正则匹配函数
regexp 功能和 rlike类似
select count(*) from olap_b_dw_hotelorder_f where create_date_wid regexp '\\d{8}'
select count(*) from olap_b_dw_hotelorder_f where create_date_wid rlike '\\d{8}'
regexp_extract(string subject, string pattern, int index) 将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符
select regexp_extract('IloveYou','(I)(.*?)(You)',1)
from test1 limit 1 // 1 匹配 I,2匹配 love,3匹配 You
regexp_replace(string
A, string B, string C) 将字符串A中的符合Java正则表达式B的部分替换为C
select regexp_replace("IloveYou","You","")
from test1 // Ilove
这里几个函数类似oracle的5个正则函数,具体可参考oracle的正则匹配函数