【问题标题】:how to use regexp_extract in hive如何在 hive 中使用 regexp_extract
【发布时间】:2019-07-26 01:40:43
【问题描述】:

我正在尝试使用 regexp_extract 提取以下字符串的一部分,但没有成功:

CUST_NEW_ACCOUNTS_LINES_2019-03-03.dat.gz

我只想获取日期部分。在 regex101.com 网站上,这似乎可行,但 hive 给了我一条错误消息。

regexp_extract(meta_source_filename,'^(?:[^_]+_){4}([^_]+)') file_date

有人可以帮我理解这里的错误吗?我完全不熟悉 regexp_extract 语法,所以一直使用另一个函数作为起点。
谢谢!

【问题讨论】:

    标签: sql regex hive hiveql


    【解决方案1】:
    with your_data as (
    select 'CUST_NEW_ACCOUNTS_LINES_2019-03-03.dat.gz' str
    )
    
    select regexp_extract(str,'_(\\d{4}(-\\d{2}){2})\\.',1)
    from your_data;
    

    结果:

    OK
    2019-03-03
    Time taken: 0.062 seconds, Fetched: 1 row(s)
    

    表达式'_(\\d{4}(-\\d{2}){2})\\.' 表示:

    下划线_四位数字\\d{4}重复(连字符和两位数字)两次(-\\d{2}){2}\\.

    捕获第一组(仅限日期):(\\d{4}(-\\d{2}){2})。 在 Hive 中,您需要使用 \\ 进行屏蔽。

    【讨论】:

      【解决方案2】:

      您已将所需的子字符串捕获到捕获组中。您应该使用组的编号、ID 作为第三个参数:

      regexp_extract(meta_source_filename,'^(?:[^_]+_){4}([^_]+)', 1) file_date
                                                                   ^
      

      请参阅regexp_extract(string subject, string pattern, int index) 文档:

      'index' 参数是 Java regex Matcher group() 方法的索引。有关“索引”或 Java regex group() 方法的更多信息,请参阅 docs/api/java/util/regex/Matcher.html

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多