There is a string(just for testing) and I want to replace all instances of <p> under the div, <div id="text">. How do I do that ?
I tested with m and s modifiers, but in vain (Only the first one gets replaced). I have given my Perl code below :
#!/usr/bin/perl
use strict;
use warnings;
my $string = <<STRING;
<div id="main">
hellohello
<div id="text">
nokay.
<p>This is p1, SHUD B replaced</p>
Alright
<p>This is p2, SHUD B replaced</p>
Yes 2
<p>this is P3, SHUD B replaced</p>
Okay done
bye
</div>
bye
<p>this is not under the div whose id is text and SHUDN'T b replaced</p>
</div>
STRING
my $str_bak = $string;
print "Sring is : \n$string\n\n";
$string =~ s/(<div id="text">.*?)<p>(.*)(<\/p>.*?<\/div>)/$1<p style="text-align:left;">$2 $3/sig;
print "Sring now is : \n$string\n\n";
Thank you all for the help.
I could find a regex for that. So I did it with a “workaround”. This is how :
So basically that regex is applicable only to the first match and thats why we are making it repeated in an empty while loop( the loop exits when there are no more match to replace).