在PL / SQL中替换多行regexp函数

时间:2014-09-05 12:14:35

标签: sql regex oracle plsql

有没有办法从字符串中删除多行? 我有一个包含大量字符串的varchar值:

declare
  v_txt varchar(4000);
  v_txt2 varchar(4000);
begin
  v_txt := '<Html>
          <Head>
          <meta charset="windows-1251" />
          <style>
             div {text-indent: 40px;}
          </style>
          <style>
             p {text-indent: 40px;}
          </style>
          </Head>
          <Body>
          <font face="Arial">'

  select regexp_replace(v_txt, some_pattern, '') into v_txt2 from dual;
  dbms_output.put_line(v_txt2);
end;

我需要位于第一个&#39;之间的线条。标签和最后的&#39; / style&#39;要删除的标签? 我该如何实现它?用什么&#34; some_pattern&#34;?

2 个答案:

答案 0 :(得分:2)

您不需要使用REGEXP:

with t(val) as(
  select '<Html>
          <Head>
          <meta charset="windows-1251" />
          <style>
             div {text-indent: 40px;}
          </style>
          <style>
             p {text-indent: 40px;}
          </style>
          </Head>
          <Body>
          <font face="Arial">' from dual 
)
select substr(val, instr(val, '<style>'), instr(val, '</style>', -1, 1) - instr(val, '<style>', 1, 1) + length('</style>')) val
  from t

                                      VAL
---------------------------------------------------------------------------------
<style> div {text-indent: 40px;} </style> <style> p {text-indent: 40px;} </style>

答案 1 :(得分:1)

使用正则表达式解决方案:

select regexp_replace(v_txt, '<style>.*</style>', '[REPLACED]', 1, 1, 'n') into v_txt2 from dual;

结果是:

<Html>
          <Head>
          <meta charset="windows-1251" />
          [REPLACED]
          </Head>
          <Body>
          <font face="Arial">