Sql Oracle regexp_替换完整单词

Sql Oracle regexp_替换完整单词,sql,regex,oracle11g,Sql,Regex,Oracle11g,我想用“of”来代替“of”这个词。我只想让它对完整的单词起作用。所以不是在L_OF,DOF,OF z,DOFD,OF L,等等 除了最后一个字符串,我的代码在下面工作 它返回: ("OF"*OF + 2) 。。。而不是: ("OF"*"OF" + 2) 我怎样才能让它也在那个上面工作呢 with stg as ( select '(ofof+ol)' str from dual union all select '(oof+ol+of)' from dual union all s

我想用“of”来代替“of”这个词。我只想让它对完整的单词起作用。所以不是在L_OF,DOF,OF z,DOFD,OF L,等等

除了最后一个字符串,我的代码在下面工作

它返回:

("OF"*OF + 2) 
。。。而不是:

("OF"*"OF" + 2) 
我怎样才能让它也在那个上面工作呢

with stg as
(
select '(ofof+ol)' str from dual union all
select '(oof+ol+of)'   from dual union all
select '(*of + 2)'     from dual union all
select '(of*of + 2)'   from dual 
)
select str,
       regexp_replace(upper(str), '(\W|^)(OF)(\W|$)', '\1"OF"\3') as str2
from   stg

这篇评论太长了。我不知道解决办法,但我确实理解问题所在。您将看到使用的的”的”的”的”的**的”会容易得多


问题是定义第一个单词的字符不用于定义第二个单词。正则表达式似乎需要一个特殊字符,如“^”来表示“上一次匹配后的第一个字符”。我不知道它是否存在。

这里有一种方法可以做到这一点——使用递归查询(需要Oracle 11.2或更高版本)。别指望会很快

with stg as
(
  select '(ofof+ol)' str from dual union all
  select '(oof+ol+of)'   from dual union all
  select '(*of + 2)'     from dual union all
  select '(of*of + 2)'   from dual 
),
rec ( str, lvl, new_str ) as
(
  select str, 1, upper(str)
    from stg
  union all
  select str, lvl + 1, 
         regexp_replace(new_str, '(\W|^)(OF)(\W|$)', '\1"OF"\3', 1, lvl)
  from   rec
  where  regexp_instr(new_str, '(\W|^)(OF)(\W|$)', 1, lvl) > 0
)
select str, new_str
from   rec
where  regexp_instr(new_str, '(\W|^)(OF)(\W|$)', 1, lvl) = 0
;

STR          NEW_STR          
------------ ------------------
(ofof+ol)    (OFOF+OL)         
(oof+ol+of)  (OOF+OL+"OF")     
(*of + 2)    (*"OF" + 2)       
(of*of + 2)  ("OF"*"OF" + 2)   

问题在于posix正则表达式中缺少lookarounds。我认为要得到您想要的,您必须将其放入plsql过程中,循环或嵌套regexp_替换,然后在另一个替换中删除多余的引号。