【问题标题】:Oracle SQL: Use REGEXP_SUBSTR to extract text before specific wordsOracle SQL:使用 REGEXP_SUBSTR 提取特定单词之前的文本
【发布时间】:2019-03-21 04:34:24
【问题描述】:

使用 Oracle sql,我想提取某些单词之前的文本。我需要排除以“exceed”或“max”开头和之后的文本。

示例:“4-6 小时服用 1-2 个标签。每天不要超过 5 个标签”

期望的输出:“4-6 小时服用 1-2 个标签。不要”

【问题讨论】:

    标签: sql regex oracle


    【解决方案1】:

    我在想regexp_instr()substr() 而不是直接regexp_substr()

    select x.*,
           substr(str, 1, regexp_instr(str || 'max', 'exceed|max') - 2)
    from (select 'Take 1-2 tabs 4-6 hours. Do not exceed 5 tabs per day' as str from dual) x
    

    【讨论】:

      【解决方案2】:

      我通过使用嵌套的regexp_substr() 实现了所需的输出,如下所示:

      select trim(regexp_substr(
          regexp_substr('Take 1-2 tabs 4-6 hours. Do not exceed 5 tabs per day','.*(exceed|max)'),
          '.*[^(exceed|max)]'
        )) desired_output
      from dual;
      

      但如果您需要“超出”或“最大”之前的空格,只需删除 trim()

      参考:https://www.techonthenet.com/oracle/functions/regexp_substr.php

      【讨论】:

        猜你喜欢
        • 2020-09-15
        • 1970-01-01
        • 2021-09-16
        • 1970-01-01
        • 2021-08-04
        • 1970-01-01
        • 1970-01-01
        • 2021-08-01
        • 1970-01-01
        相关资源
        最近更新 更多