Hive Regular Expression

Source: Internet
Author: User

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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.