Home > front end >  REGEXP_REPLACE - two letters in a string
REGEXP_REPLACE - two letters in a string

Time:08-01

I have a string:

string := '</br>, <br/>, \n<br>, jane, brutus'

I have to replace all 'br' marks by '/n' mark. I need an output like this:

'/n, /n, n/, jane, brutus'

I tried:

select regexp_replace('</br>, <br/>, \n<br>, ania', '^.*((br)[^,] )', '\n', 1) from dual;

But then output is like this:

'/n'

Any ideas how can I resolve this problem?

CodePudding user response:

It is easier than you thought. Just look for the 'br> parts. This is: an opening angle bracket, an optional slash, the letters b and r, an optional slash, a closing angle bracket: </?br/?>.

The whole expression:

select regexp_replace('</br>, <br/>, \n<br>, ania', '</?br/?>', '\n') from dual;

If you want to replace all groups of adjacent marks and treat br marks the same as \n in your search, then it gets a little bit more complicated:

regexp_replace('</br>, <br/>, \n<br>, ania', '(\\n|</?br/?>) ', '\n')

CodePudding user response:

If I understood correctly this code will be work.

SQL>
SELECT word,
   REGEXP_REPLACE (REGEXP_REPLACE (word, '([\\<])([a-zA-Z/] ) 
                    ([>])', '/n'),
                   '\\n([a-zA-Z/] )',
                   '\1')
      AS Result
   FROM (SELECT '</br>, <br/>, \n<br>, jane, brutus' AS word FROM DUAL)

RESULT

/n, /n, /n, jane, brutus

SQL>

  • Related