【问题】
需要把对应的html代码:
h1 content
div test
中的标签用perl的正则去替换掉。
【解决过程】
1.参考了:
最后写出如下代码:#!/usr/bin/perl -w
=File decalaration
Function:
求perl过滤html标签的函数,或正则表达式
http://zhidao.baidu.com/question/510205886.html
Author: Crifan Li
Version: 2012-12-24
Contact: admin at crifan dot com
=cut
use warnings;
$origHtml = <
h1 content
div test
invalid tag test
END
print("origHtml=", $origHtml);
$filteredHtml = $origHtml;
$filteredHtml =~ s/(.+?)/$2/sg;
#$filteredHtml =~ s/(.+?)/$1/sg; # will also remove invalid tag
print "after remove tag=",$filteredHtml;
# h1 content
# div test
# invalid tag test
【总结】
Perl中的正则:
1.替换$variable = "xxx";
$variable =~ s/yyy/zzz/flags;
注意:字符串变量必须先初始化,否则u会报错:Use of uninitialized value $_ in substitution (s///) at xxx.pl line 25.
flags:就是正常的参数,详见:Perl正则表达式
2.后向引用
使用$N,其中N=1,2,3,…,对应着组的编号
3. 总之,Perl中的正则,还是不好用啊。目前觉得,比较好用的正则,是C#,Python等,功能丰富,方便使用。