3

尝试将tr具有 a 的项目classeveor开头的前三个字母匹配day。这是我的尝试:

my @stuff = $p->look_down(
    _tag => 'tr',
    class => 'qr/eve*|day*/g'
);

foreach (@stuff) {
        print $_->as_text;
};

只是好奇,什么样的物体在里面@stuff


这个可以吗?见下文:

my @stuff = $p->look_down(
    _tag => 'tr',
    class => qr/eve.*|day.*/
);

print "\n\n";

foreach (@stuff) {
        print $_->as_text . "\n\n";
};
4

1 回答 1

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
于 2014-05-30T01:25:09.237 回答