2014-01-13 71 views
0

我想删除特定的一组HTML标记,这里是我曾尝试删除特定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文件,只拿到两行 - 一个是页眉和2号线有得到了包括几张桌子的全部内容。现在我试图删除一些不需要的表格关闭选项卡,这些选项卡可以帮助我将表格合并到一起。不幸的是,它将删除替换字符串后的所有内容我哪里错了?

+1

为了让你更清楚你想要达到的目标,也许你可以添加一个你的输入可能看起来像什么和你的输出应该是什么样子的例子。 – Simon

+0

@Simon这里是链接到html页面http://codepad.org/Wu0ItDHI 这里是我期待的http://codepad.org/jpcYbGHz –

+1

我不确定,但如果你想删除一些在html中的标签,你可以使用HTML解析器 – gaussblurinc

回答

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; 
} 

+0

这没有帮助,无论是在str_rep被取代后 –

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

一切工作正常!