oracle - SQL - Update String in Colum with regex -


using sql, trying update substring in column regexp.

that's goal :

  • i have html content in column : example :

    <a href="http://blabla.fr/blibli">link1</a> 

or other content multiple links:

  <p>blablabla</p>   <a href="http://blabla.fr/blibli">link1</a>   <a href="http://blabla.fr/">link2</a>   <a href="http://blabla.fr/test">link3 </a> 

(this example represente entire string, not many result of select ) - want remove 'http://' in urls not contains 'blibli'

so did query

 update my_table set my_col = replace(my_col,'http://','') not regexp_like(my_col,'http://^((?blibli).)*$') 

but work if column contains @ least 1 link first example. if column contains many link, update nothing due conditions. (maybe doing many step? ) tried have regexp_substr think can't achieve too. desired result second example :

<p>blablabla</p> <a href="http://blabla.fr/blibli">link1</a> <a href="blabla.fr/">link2</a> <a href="blabla.fr/test">link3 </a> 

thanks advice can give me

similar trung's approach, capturing both (either) matched group:

update my_table set my_col = regexp_replace(my_col,   'href="(http://[^"]*blibli[^"]*)"|href="http://([^"]*)"', 'href="\1\2"') 

optionally where clause avoid updating rows don't have href, say.

quick demo of how sample values (supplied in cte) translated:

with my_table (id, my_col) (   select 1, '<a href="http://blabla.fr/blibli">link1</a>' dual   union select 2, '<a href="http://blabla.fr/blibli/abc">link1</a>' dual   union select 3, '<a href="http://blabla.fr/xbliblix">link1</a>' dual   union select 4, '<a href="http://blabla.fr/test">link1</a>' dual   union select 5, '<p>blablabla</p> <a href="http://blabla.fr/blibli">link1</a> <a href="http://blabla.fr/">link2</a> <a href="http://blabla.fr/test">link3 </a>' dual ) select id, my_col, regexp_replace(my_col,     'href="(http://[^"]*blibli[^"]*)"|href="http://([^"]*)"',     'href="\1\2"') result my_table;          id my_col                                           result                                           ---------- ------------------------------------------------ ------------------------------------------------          1 <a href="http://blabla.fr/blibli">link1</a>      <a href="http://blabla.fr/blibli">link1</a>               2 <a href="http://blabla.fr/blibli/abc">link1</a>  <a href="http://blabla.fr/blibli/abc">link1</a>           3 <a href="http://blabla.fr/xbliblix">link1</a>    <a href="http://blabla.fr/xbliblix">link1</a>             4 <a href="http://blabla.fr/test">link1</a>        <a href="blabla.fr/test">link1</a>                        5 <p>blablabla</p>                                 <p>blablabla</p>                                            <a href="http://blabla.fr/blibli">link1</a>      <a href="http://blabla.fr/blibli">link1</a>                 <a href="http://blabla.fr/">link2</a>            <a href="blabla.fr/">link2</a>                              <a href="http://blabla.fr/test">link3 </a>       <a href="blabla.fr/test">link3 </a>              

Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -