【问题标题】:Hive: Extract string between first and last occurrence of a characterHive:在字符的第一次和最后一次出现之间提取字符串
【发布时间】:2017-08-20 09:20:41
【问题描述】:

我有一个 Hive 表列,它的字符串由“-”分隔,我需要提取第一次和最后一次出现“-”之间的字符串

    +-----------------+
    | col1            |
    +-----------------+
    | abc-123-na-00-sf|
    | 123-abc-01-sd   |
    | 123-abcd-sd     |  
    +-----------------+

    Required output:

    +-----------+
    | col1      |
    +-----------+
    | 123-na-00 |
    | abc-01    |
    | abcd      |  
    +-----------+

请建议一些正则表达式来提取所需的输出。

谢谢

【问题讨论】:

    标签: regex hadoop hive hiveql


    【解决方案1】:
    with t as (select explode(array('abc-123-na-00-sf','123-abc-01-sd','123-abcd-sd')) as str)
    select  regexp_extract (str,'-(.*)-',1)
    from    t
    ;
    

    123-na-00
    abc-01
    abcd
    

    with t as (select explode(array('abc-123-na-00-sf','123-abc-01-sd','123-abcd-sd')) as str)
    select  regexp_extract (str,'(?<=-).*(?=-)',0)
    from    t
    ;
    

    123-na-00
    abc-01
    abcd
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 2013-12-28
      • 1970-01-01
      • 2015-05-11
      • 2022-01-13
      相关资源
      最近更新 更多