[英]How can I get all matches of a regex in a string?
如何使用curl
獲取任何html標簽的內容? 在以下腳本中獲取例如h1
內容:
#!/usr/bin/perl
use strict;
use warnings;
my $page = `curl www.yahoo.com`;
print "Page: \n";
sleep(5);
#print "$page \n";
if ($page =~ m/<h1\s*>(.*)<\/h1\s*>/ig){
print "$1 \n";
}
我只有一場比賽。 如何獲得所有比賽?
您可以像這樣獲得所有匹配項:
my @matches = $page =~ /<h1\b[^>]*>(.*?)<\/h1>/ig;
print "@matches\n";
用正則表達式解析HTML是一種罪過 。 幸運的是,周圍有很多解析器。 我特別喜歡Mojo套件:
use strict; use warnings;
use feature 'say';
use Mojo;
my $ua = Mojo::UserAgent->new(max_redirects => 5); # redirects defaults to zero
for my $h3 ($ua->get('www.stackoverflow.com')->res->dom('h3')->each) { # use CSS selectors
say $h3->all_text;
}
使用while循環代替if :
while ($page =~ m/<h1\s*>(.*)<\/h1\s*>/ig) {
print "$1 \n";
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.