Skip to content
Advertisement

How to replace the word which is not placed between a pattern using Oracle REGEXP and not the same word inside the pattern?

I am trying to replace the word “group” when it’s not between the pattern <a href and a> with “group1” . Below query replaces “group” inside the desired pattern. How to replace the word which is just outside the pattern?

with t as (
    select '<a href Part of the technical Network Group www.tech.com/sites/ hh a> group' as text from dual
    union all select '<a href mean www.tech.technical Network a>' as text from dual
    union all select 'www.tech.tech///technical <a href Network Group a>' as text from dual)
select regexp_replace(text,'group','group1',1,0,'i')
from t
WHERE REGEXP_LIKE(text,'<a href.*group.*a>','i') 

The expected output for the first row is (the word “group” appears inside and outside the pattern). The expectation is to just replace the one which is outside)

<a href Part of the technical Network group www.tech.com/sites/ hh a> group1

Advertisement

Answer

I would be remiss if I did not point everyone reading this to the definitive post on parsing HTML with regex’s: RegEx match open tags except XHTML self-contained tags

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement