首页 > 解决方案 > Oracle SQL:使用 REGEXP_SUBSTR 提取特定单词之前的文本

问题描述

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

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

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

标签: sqlregexoracle

解决方案


我通过使用 nested 实现了所需的输出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


推荐阅读