0

Is there any way to delete several lines from a string? I have a varchar value with a large amount of strings in it:

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;

I need lines situated between the first 'style' tag and the last '/style' tag to be deleted? How can i implement it? With what "some_pattern"?

2 Answers 2

2

You don't need to use 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>
Sign up to request clarification or add additional context in comments.

1 Comment

You gave me a nice idea. Thanks a lot. I've achieved what i wanted.
1

Solution with regexp:

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

Result is:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.