The implementation of regular expressions in hive is different from that in Java:
Here we have summarized some of the following points:
The regular expressions in hive can be used, but the difference is that the original escape character '\' is changed to a double slash '\\'
The regular expression parsing function in hive: regexp_extract; for example, 'match 10.122.248'
Select regexp_extract (host, '(^ [\ W] + )\\. ([\ W] + )\\. ([\ W] +) ', 0) AA from browsewebpagelog where dt like '123 ';
The first parameter: The field to be processed, the second parameter needs to match the regular expression, and the third parameter: 0 is to display the matching string, 1 is to display the first square brackets, 2 is to display the fields in the second bracket...
Syntax: regexp_extract (string subject, string pattern, int index)
Return Value: String
Description: splits the string subject according to the Regular Expression Pattern and returns the character specified by index. Note: In some cases, use escape characters
Example:
Hive> select regexp_extract ('foothebar', 'foo (.*?) (Bar) ', 1) from dual;
The
Hive> select regexp_extract ('foothebar', 'foo (.*?) (Bar) ', 2) from dual;
Bar
Hive> select regexp_extract ('foothebar', 'foo (.*?) (Bar) ', 0) from dual;
Foothebar
Note: Some blogs use rlike to match regular expressions. I have tried to match the regular expressions as long as they appear, you must add the start '^' symbol. Otherwise, the matching result is definitely not expected.