有一个字符串(仅用于测试),我想替换<p>
div 下的所有实例,<div id="text">
. 我怎么做 ?
我用m
ands
修饰符进行了测试,但徒劳无功(只有第一个被替换)。我在下面给出了我的 Perl 代码:
#!/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";