hive 内置正则匹配函数 regexp |
您所在的位置:网站首页 › hiveregexp函数使用方法 › hive 内置正则匹配函数 regexp |
1. regexp_replace
先看下官网定义: 返回值函数格式解释string regexp_replace(string INITIAL_STRING, string PATTERN, string REPLACEMENT) Returns the string resulting from replacing all substrings in INITIAL_STRING that match the java regular expression syntax defined in PATTERN with instances of REPLACEMENT. For example, regexp_replace("foobar", "oo|ar", "") returns 'fb.' Note that some care is necessary in using predefined character classes: using '\s' as the second argument will match the letter s; '\\s' is necessary to match whitespace, etc. 需要注意的是这里使用正则规定的符号时需要多加一个反斜杠 '\',比如想要匹配数据,就需要用'\\d'来匹配 在自己试验时发现该函数也可以做到分组捕获,使用'\$1'来匹配第一个分组,以此类推,直接看例子: -- 分组捕获 select regexp_replace('123-456-789',"(\\d+)-(\\d+)-(\\d+)",'\$1+\$3+'); 123+789 -- 匹配空白字符 select regexp_replace('hello world!',"\\s",'-'); hello-world!2.regexp_extract 官方解释: string regexp_extract(string subject, string pattern, int index) Returns the string extracted using the pattern. For example, regexp_extract('foothebar', 'foo(.*?)(bar)', 2) returns 'bar.' Note that some care is necessary in using predefined character classes: using '\s' as the second argument will match the letter s; '\\s' is necessary to match whitespace, etc. The 'index' parameter is the Java regex Matcher group() method index. See docs/api/java/util/regex/Matcher.html for more information on the 'index' or Java regex group() method.
重点也是在使用正则内置变量时需要多加一个反斜杠进行转义,比如匹配空白字符需要用'\\s'; 最后一个入参index就是java正则匹配的group()方法的入参,入参只能是一个整数,也就是每次只能取到一个分组 看例子: --获取匹配的第2个分组 select regexp_extract('123-456-789','(\\d+)-(\\d+)-(\\d+)',2); 456
|
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |