in reply to Re: substitute character
in thread substitute character

#!/usr/bin/perl my $line; while (my $line = <DATA>){ if($line =~ m/<span><p>(.*)/){ $line= $1; $line =~ s/</<\/p><p>/g; print $line; } } __DATA__ <span><p>This affected me deeply because < I was being treated for dep +ression then. < felt she was replacing me as mother and grandmother.
suppose the input is like above specified. How to print the full line with substitution of <p> tags. Now the above code prints like
This affected me deeply because </p><p> I was being treated for depres +sion then. </p><p> felt she was replacing me as mother and grandmothe +r.
But I want the line similar to this
<span><p>This affected me deeply because </p><p> I was being treated f +or depression then. </p><p> felt she was replacing me as mother and g +randmother.

Replies are listed 'Best First'.
Re^3: substitute character
by arun_kom (Monk) on Sep 02, 2009 at 09:18 UTC
    Consider this instead if your idea is to capture text between any kind of html start and end tag pair.

    if($line =~ m/<.+>(.+)<\/.+>/){ print $1; }

    UPDATE: I see now that the question i replied to has been replaced by you ... probably updated. So my answer above doesn't hold.

      If I use substitution of $1, then <span><p> is removed from the input data which printing the output. In the script
      #!/usr/bin/perl my $line; while (my $line = <DATA>){ if($line =~ m/<span><p>(.*)/){ $line = $1; $line =~ s/</<\/p><p>/g; print $line; } #} } __DATA__ <span><p>This affected me deeply because < I was being treated for dep +ression then. < felt she was replacing me as mother and grandmother.
      From the above code the output is below.
      This affected me deeply because </p><p> I was being treated for depres +sion then. </p><p> felt she was replacing me as mother and grandmothe +r.
      But I have to print the line, such that it does substitution
      <span><p>This affected me deeply because </p><p> I was being treated f +or depression then. </p><p> felt she was replacing me as mother and g +randmother.

        See perlre on backreferences, but in the long run, you want something like HTML::TokeParser or HTML::TreeBuilder, because modifying HTML using regular expressions only works for reasonably well-formed HTML.

          A reply falls below the community's threshold of quality. You may see it by logging in.