或匹配HTML :: TreeBuilder的look_down功能

时间:2014-05-30 01:16:13

标签: regex perl html-treebuilder

尝试匹配tr项<{1}}与的项目classeve开头的前三个字母。这是我的尝试:

day

好奇,my @stuff = $p->look_down( _tag => 'tr', class => 'qr/eve*|day*/g' ); foreach (@stuff) { print $_->as_text; }; 中有哪些对象?


这样好吗?见下文:

@stuff

1 个答案:

答案 0 :(得分:5)

您需要使用^锚定正则表达式,以使类与前三个字母匹配。

以下内容可实现您的目标:

use strict;
use warnings;

use HTML::TreeBuilder;

my $p = HTML::TreeBuilder->new_from_content(do {local $/; <DATA>});

foreach my $tr ($p->look_down(_tag => 'tr', class => qr{^(?:eve|day)})) {
    print $tr->as_text, "\n";
};

__DATA__
<html>
<body>
<p>hi</p>
<table>
<tr class="notme"><td colspan=2>row 1 is bad</td></tr>
<tr class="not_eve_or_day"><td colspan=2>row 2 is bad</td></tr>
<tr class="everyrow"><td colspan=2>row 3 is good 1 of 2</td></tr>
<tr class="dayme"><td colspan=2>row 4 is good 2 of 2</td></tr>
<tr class="notme"><td colspan=2>row 5 is bad</td></tr>
</table>
</body>
</html>

输出:

row 3 is good 1 of 2
row 4 is good 2 of 2