OR match for HTML::TreeBuilder's look_down feature

734 views Asked by At

Trying to match tr items that have a class with either the first three letters starting with eve or day. This is my attempt:

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

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

Just curious, what kind of objects are in @stuff?


Is this OK? See below:

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

print "\n\n";

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

There are 1 answers

5
Miller On

You need to anchor your regex with ^ in order for the class to match the first three letters.

The following achieves what you want:

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>

Outputs:

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