Hive 正则匹配函数 regexp_extract

1。regexp_extract

语法:    regexp_extract(string subject,  string pattern,  int index)

返回值: string

说明:  将字符串subject按照pattern正则表达式的规则拆分,返回index指定的字符。

第一参数:   要处理的字段

第二参数:    需要匹配的正则表达式

第三个参数:

  • 0是显示与之匹配的整个字符串
  • 1 是显示第一个括号里面的
  • 2 是显示第二个括号里面的字段...

注意,在有些情况下要使用转义字符(双斜杠了‘\’)。

举例:

1
2
3
4
5
6
7
8
9
10
11
select
regexp_extract('x=a3&x=18abc&x=2&y=3&x=4','x=([0-9]+)([a-z]+)',0),  -- x=18abc
regexp_extract('x=a3&x=18abc&x=2&y=3&x=4','^x=([a-z]+)([0-9]+)',0), -- x=a3
 
regexp_extract('https://detail.tmall.com/item.htm?spm=608.7065813.ne.1.Ni3rsN&id=522228774076&tracelog=fromnonactive','id=([0-9]+)',0),    -- id=522228774076
regexp_extract('https://detail.tmall.com/item.htm?spm=608.7065813.ne.1.Ni3rsN&id=522228774076&tracelog=fromnonactive','id=([0-9]+)',1),    -- 522228774076
 
regexp_extract('http://a.m.taobao.com/i41915173660.htm','i([0-9]+)',0),            -- i41915173660
regexp_extract('http://a.m.taobao.com/i41915173660.htm','i([0-9]+)',1)             -- 41915173660
 
from test.dual;

正则匹配字符解释:

  • ^ 表示开头
  • $ 表示结尾
  • . 表示任意字符
  • * 表示任意多个

2。手机号清洗:

 regexp_replace(mobile,'[^0-9]','')    regexp '1[0-9]{10}'  返回true,则为好的手机号。

具体使用:

select id,name,corse,time,
regexp_extract(name,'li":(.*?),"z',1) as price,
from student
where time='20170703'
limit 100

原文地址:https://www.cnblogs.com/oraser/p/7648012.html