我想删除一组特定的html标签,这是我试过的
$str_rep="<table></td></tr></table></td></tr></table></td></tr>";
local $^I = ""; # Enable in-place editing.
push(@files,"$report_file");
local @ARGV = @files; # Set files to operate on.
while (<>) {
s/(.*)$str_rep(.*)$/$1$2/g;
print;
}
Html文件只有两行 - 一个是页眉,第二行有完整的内容,包括几个表。现在我试图删除一些不需要的表关闭选项卡,这些选项卡可以帮助我将表合并在一起。不幸的是它在更换字符串后删除了所有内容。我哪里错了?
答案 0 :(得分:0)
你应该转义斜杠/
,只需用空字符串替换匹配的字符串:
$str_rep="<table><\/td><\/tr><\/table><\/td><\/tr><\/table><\/td><\/tr>";
local $^I = ""; # Enable in-place editing.
push(@files,"$report_file");
local @ARGV = @files; # Set files to operate on.
while (<>) {
s/$str_rep//g;
print;
}
答案 1 :(得分:0)
你在这里:
my $report_file = 'input.html';
# see at this v - you forget about one \/ near table :)
my $str_rep="<\/table><\/td><\/tr><\/table><\/td><\/tr><\/table><\/td><\/tr>";
local $^I = ""; # Enable in-place editing.
push(@files,"$report_file");
local @ARGV = @files; # Set files to operate on.
while (<>) {
s/$str_rep//g;
print;
}
我将diff用于input.html和target.html
一切正常!