$match = q(<a href="#google"><h1><b>Google</b></h1></a>);
if($match =~ /<a.*?href.*?><.?>(.*?)<\/a>/){
$title = $1;
}else {
$title="";
}
print"$title";
输出:Google</b></h1>
应该是:Google
无法在 Perl 中使用正则表达式从链接中提取值,它可能有一个或多或少的嵌套:
<h1><b><i>Google</i></b></h1>
请试试这个:
1)
2) 惠普
3) 通用
4) [1]
输出:
Unix 外壳
生命值
一般
[1]
最佳答案
如评论中所述,不要使用正则表达式。我特别喜欢 Mojo suite ,这让我可以使用 CSS 选择器:
use Mojo;
my $dom = Mojo::DOM->new(q(<a href="#google"><h1><b>Google</b></h1></a>));
print $dom->at('a[href="#google"]')->all_text, "\n";
use HTML::TreeBuilder::XPath;
my $dom = HTML::TreeBuilder::XPath->new_from_content(q(<a href="#google"><h1><b>Google</b></h1></a>));
print $dom->findvalue('//a[@href="#google"]'), "\n";
关于html - Perl 正则表达式从嵌套的 html 标签中提取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18488539/