使用Oracle sql,我希望提取某些单词之前的文本。我需要排除以单词"exceed“或"max”开头和之后的文本。
例如:“服用1-2片4-6小时,每天不要超过5片”
期望的输出:“拿1-2个标签4-6个小时。不要”
发布于 2018-10-16 23:27:27
我在考虑使用substr()
的regexp_instr()
,而不是直接使用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
发布于 2018-10-17 14:52:02
我通过使用嵌套的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
https://stackoverflow.com/questions/52838913
复制相似问题