【发布时间】:2019-03-21 04:34:24
【问题描述】:
使用 Oracle sql,我想提取某些单词之前的文本。我需要排除以“exceed”或“max”开头和之后的文本。
示例:“4-6 小时服用 1-2 个标签。每天不要超过 5 个标签”
期望的输出:“4-6 小时服用 1-2 个标签。不要”
【问题讨论】:
使用 Oracle sql,我想提取某些单词之前的文本。我需要排除以“exceed”或“max”开头和之后的文本。
示例:“4-6 小时服用 1-2 个标签。每天不要超过 5 个标签”
期望的输出:“4-6 小时服用 1-2 个标签。不要”
【问题讨论】:
我在想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
【讨论】:
我通过使用嵌套的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
【讨论】: