gpt4 book ai didi

regex - 或匹配 HTML::TreeBuilder 的 look_down 功能

转载 作者:行者123 更新时间:2023-12-01 11:38:52 24 4
gpt4 key购买 nike

尝试将具有 classtr 项目与 或者eve 开头的前三个字母或。这是我的尝试:

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";
};

最佳答案

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

以下实现你想要的:

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

关于regex - 或匹配 HTML::TreeBuilder 的 look_down 功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23945578/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com