Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OR match for HTML::TreeBuilder's look_down feature

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";
};
like image 225
user3689651 Avatar asked Dec 22 '25 04:12

user3689651


1 Answers

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
like image 95
Miller Avatar answered Dec 23 '25 23:12

Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!