您好我有一個名爲HTML文件1.HTML這樣如何使用Perl兩條特定線
<div class="t_l"></div>
<some>
lines
of
codes
</some>
<div class="t_r"></div>
我要替換的div到另一個,它存儲在內容文件稱爲「橫幅」。 橫幅文件是
<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;
上面的代碼不能工作。有什麼建議麼?
你應該使用其中之一perl HTML解析器模塊,例如'HTML :: TokeParser'。 – David
你真的不想用正則表達式解析HTML。那就是瘋狂: - / –