创建一个要捕获的图案,其后面或前面没有字母或数字

问题描述 投票:0回答:1

我正在使用 REGEXP_REPLACE 来替换前面或后面没有数字或字符的字符集。 基本上我的要求是删除单词 PT,但前提是它不用于包含数字和字母的单词中间。

'研究论文 PT' --> '研究论文'

'研究论文PT' --> '研究论文PT'

我发现oracle不支持负lookahead和lookbehind。 有没有办法实现这个目标?

regex oracle-database plsql regexp-replace
1个回答
0
投票

作为起点(添加更多测试):

with data(id, str) as (
    select 1, 'PTResearch Thesis PT' from dual union all
    select 2, 'ResePTarch ThesisPT PT bla' from dual union all
    select 3, 'Research Thesis PT' from dual union all
    select 4, 'Research ThesisPT' from dual union all
    select 5, 'PT Research Thesis PT' from dual -- union all
)
select id, str, listagg(case when word <> 'PT' then word || sep end, '') within group(order by lvl) as res
from (
    select id, str, level as lvl, regexp_substr(str, '([[:alnum:]]+)', 1, level) as word,
        regexp_substr(str, '([^[:alnum:]]+)', 1, level) as sep
    from data 
    connect by prior id = id and regexp_substr(str, '([[:alnum:]]+)', 1, level) is not null
        and prior sys_guid() is not null
)
group by id, str

;
© www.soinside.com 2019 - 2024. All rights reserved.