您的位置:首页 > 其它

使用perl的正则表达式对文件中的特定类型超链接里面的换行去掉,使其在一行

2012-11-22 10:14 477 查看
感谢大学里面的王仲老师,他给我的影响很大,他教给我很重要的一点是要学习使用perl,当时我只是借了一本图书学习了一下,不曾运行过一句代码。

现今遇到难题了,才想起perl的强大来。如果说就我熟悉的语言来做比较,js擅长正则,java擅长文件,那么perl就是两者兼之。

以前没用perl,我考虑过将js与java通过web连接起来,以取长补短,但感觉还是太麻烦,并且那对于单文件还可以应付,如果要遍历整个目录,肯定够呛,所以我将目光投向了perl,以前只是纸上谈兵,现在要开始实战了。

问题:将下面这段代码

<td background='${imagePath}tabbg.gif' class='Tab_ContentTd'>
<a href="<@ww.url action="listChildBizModules">s23234
<@ww.param name="parentBizModuleId" value="bizModule.id"/>
</@ww.url>">
${action.getText("child.bizmodule.list")}
</a>sldfjslkdfjlskfl

<a href="<@ww.url action="listChildBizModules">s
<@ww.param name="parentBizModuleId" value="bizModule.id"/>
</@ww.url>">${action.getText("child.bizmodule.list")}
</a>
</td>
<td><img src="${imagePath}tabright.gif" class="Tab_HotRight"></td>


换成下面的形式

<td><img src='${imagePath}tab_left.gif' class="Tab_HotLeft"></td>
<td background='${imagePath}tabbg.gif' class='Tab_ContentTd'>
<a href="<@ww.url action="listChildBizModules">s23234<@ww.param name="parentBizModuleId" value="bizModule.id"/></@ww.url>">${action.getText("child.bizmodule.list")}</a>sldfjslkdfjlskfl

<a href="<@ww.url action="listChildBizModules">s<@ww.param name="parentBizModuleId" value="bizModule.id"/></@ww.url>">${action.getText("child.bizmodule.list")}</a>
</td>
<td><img src="${imagePath}tabright.gif" class="Tab_HotRight"></td>


即只对形如<a href="<@ww.url action超链接进行处理,使整个标签放到一行上(目的是为了让双引号的内容不至于错行,使js脚本可以正常运行),页面中的其它部分保持原样。看起来简单,实际上我所了解的任何编辑器都不能完成这项工作

下面说一下perl中的解决办法:

my $addr = 'D:\perltest\extcomponent\bizmoduleconfig\bizModule_header.ftl';
# my $addr = "c:\22.log";

if (open(FILE, $addr)) {
# here's what to do if the file opened successfully
print "open file \"$addr\" successful!\n";
}
else{
print "open file \"$addr\" unsuccessful!\n";
}

my $record;
{
local $/ = undef;// 一次性读取整个文件到变量中,参考http://perl.plover.com/local.html
$record=<FILE>;
}
close(FILE);

#找到超链接并且是使用双w的标签,使用e开关对结果进行处理再行替换
$record =~ s/<a href=\"<\@ww.*?<\/a>/subBlank($&)/esg;

open OUTFILE, ">", 'a.ftl' or die "Could not open file. $!";
print OUTFILE ($record);
close OUTFILE;

sub subBlank{
my $str = "$_[0]";
$str =~ s/\s*\R\s*//g;#去掉换行及换行前后的空格
return $str ;
}


必应了一天,终于找到对一个目录进行递归处理的方法(批量处理)

use File::Find;

my $dir = 'D:/perltest/new/extcomponent';# whatever you want the starting directory to be

find(\&do_something_with_file, $dir);

sub do_something_with_file
{
unless(-d){
# print $_ . "\n";
replaceww($_);
}
# print "\t"; #.....
}

sub replaceww{
if (open(FILE, $_)) {
# here's what to do if the file opened successfully
# print "open file \"$_\" successful!\n";
}
else{
print "open file \"$_\" unsuccessful!\n";
return;
}

my $record;
{
local $/ = undef;
$record=<FILE>;
}
close(FILE);

#找到超链接并且是使用双w的标签,使用e开关对结果进行处理再行替换
my $hasww =  $record =~ /<a href=\"<\@ww.*?<\/a>/sg;
if($hasww) {
print $hasww . "  " . $_ . "\n";

$record =~ s/<a href=\"<\@ww.*?<\/a>/subBlank($&)/esg;

# 将处理后的结果写回文件
open OUTFILE, ">", $_ or die "Could not open file. $!";
print OUTFILE ($record);
close OUTFILE;
}

sub subBlank{
my $str = "$_[0]";
$str =~ s/\s*\R\s*//g;#去掉换行及换行前后的空格
return $str ;
}

}


可以将上面的内容保存到一个文件replace_ww.pl中,然后在perl环境下执行即可,工具padre不错,也是perl官网推荐的IDE,虽然没有eclipse那样强大
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: