首页 > 解决方案 > 如何仅替换 Oracle 中文本的某些部分?

问题描述

我正在尝试替换文本中的某些单词。但是,如果它是 url 的一部分,则不希望该词被替换。例如:技术一词。它在两个地方都替换了(包括 url)。

select regexp_replace('Part of the Technical Network Group https://technical.com/sites/',
                      'technical','tech',1,0,'i')
  from dual

输出:

Part of the tech Network Group https://tech.com/sites/

预期输出:

Part of the tech Network Group https://technical.com/sites/

标签: sqloraclereplacestring-matchingregexp-replace

解决方案


分两步完成。

首先,如果单词出现在单词之间,其次如果单词是句子的第一个单词。

select 
      regexp_replace(regexp_replace('technical Part of Technical Group https://technical.com/sites/',
                     ' technical', 
                     ' tech',1,0,'i'), 'technical ', 'tech ',1,0,'i') 
  from dual

希望这能解决您的问题。


推荐阅读