hive中的正则表达式还是很强大的。数据工作者平时也离不开正则表达式。对此,特意做了个hive正则表达式的小结。所有代码都经过亲测,正常运行。
1.regexp
语法: A REGEXP B
操作类型: strings
描述: 功能与RLIKE相同
selectcount(*)fromolap_b_dw_hotelorder_fwherecreate_date_widnotregexp'\\d{8}'
与下面查询的效果是等效的:
selectcount(*)fromolap_b_dw_hotelorder_fwherecreate_date_widnotrlike'\\d{8}';
2.regexp_extract
语法: regexp_extract(string subject, string pattern, int index)
返回值: string
说明:将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符。
hive> select regexp_extract('IloveYou','I(.*?)(You)',1) from test1 limit1;
Total jobs =1
。。。
Total MapReduce CPU Time Spent:7seconds340msec
ok
love
Time taken:28.067seconds, Fetched:1row(s)
hive> select regexp_extract('IloveYou','I(.*?)(You)',2) from test1 limit1;
You
hive> select regexp_extract('IloveYou','(I)(.*?)(You)',1) from test1 limit1;
I
hive> select regexp_extract('IloveYou','(I)(.*?)(You)',0) from test1 limit1;
IloveYou
hive> select regexp_replace("IloveYou","You","") from test1 limit1;
Ilove
3.regexp_replace
语法: regexp_replace(string A, string B, string C)
返回值: string
说明:将字符串A中的符合java正则表达式B的部分替换为C。注意,在有些情况下要使用转义字符,类似oracle中的regexp_replace函数。
hive> select regexp_replace("IloveYou","You","") from test1 limit1;
Ilove
hive> select regexp_replace("IloveYou","You","lili") from test1 limit1;
Ilovelili