嗨,我有一个像这样的名为 1.html 的 html 文件
<div class="t_l"></div>
<some>
lines
of
codes
</some>
<div class="t_r"></div>
我想将该 div 的内容替换为另一个内容,该内容存储在名为“banner”的文件中。横幅文件是
<other>
lines
of some
codes
</other>
所以我想要得到的是:
<div class="t_l"></div>
<other>
lines
of some
codes
</other>
<div class="t_r"></div>
我想出使用 perl 是这样的:
# Slurp file 1.html into a single string
open(FILE,"1.html") or die "Can't open file: $!";
undef $/;
my $file = <FILE>;
open(BANNER,"banner") or die "Can't open file: $!";
undef $/;
my $banner = <BANNER>;
close BANNER;
# Set strings to find and insert
my $first_line = '<div class="t_l"></div>';
my $second_line = '<div class="t_r"></div>';
$file =~ s/$first_line\n.+?\n$second_line#s/$first_line\n$banner\n$second_line/;
close FILE;
# Write output to output.txt
open(OUTPUT,">1new.html") or die "Can't open file: $!";
print OUTPUT $file;
close OUTPUT;
上面的代码不能工作。有什么建议么?