【问题标题】:Extracting out a string given a starting and ending pattern using MySQL/Presto使用 MySQL/Presto 提取给定开始和结束模式的字符串
【发布时间】:2019-11-21 15:33:03
【问题描述】:

尝试从给定特定开始和结束模式的字符串中提取文本。

真的不知道从哪里开始。我环顾四周,试图理解正则表达式函数,但它们超出了我的想象。

表:

+----+------------------------------------+
| id |              sentence              |
+----+------------------------------------+
|  1 | Hello, I am a bird.                |
|  2 | Hello, I am a cat. I like catfood. |
|  3 | Hello, I am a dog. I like bones.   |
+----+------------------------------------+

试图提取Hello,.之间的文本

输出:

+-------------+
|  sentence   |
+-------------+
| I am a bird |
| I am a cat  |
| I am a dog  |
+-------------+

【问题讨论】:

    标签: mysql hive presto


    【解决方案1】:

    尝试在 hive 中使用 regexp_extract(col,regexp,capture_group) 函数:

    Hello,    //match "Hello," literal
    ([^.]*)  //then until first occurrence of .(period) capture as first group
    

    示例:

    hive> select regexp_extract(sentence,"Hello,([^.]*)",1)sentence from( 
              --preparing sample data
               select stack(3,'Hello, I am a bird.','Hello, I am a cat. I like catfood.','Hello, I am a dog. I like bones.')
                  as(sentence))t;
    

    结果:

    sentence
     I am a bird
     I am a cat
     I am a dog
    

    【讨论】:

    • 效果很好!谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    • 2015-08-03
    • 2011-03-02
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 2018-11-14
    相关资源
    最近更新 更多