gpt4 book ai didi

regex - perl - 如何使用 RegEx 获取所有相似的匹配子字符串

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

我想解析一个网页(来自 lynda.com)并获取所有类(class)的标题和链接。所以我用了 LWP::UserAgent获取 url,然后我尝试使用以下代码获取标题和链接:

#!/usr/bin/perl

use LWP::UserAgent;
use strict;
use warnings;

my $ua = new LWP::UserAgent;
my $response = $ua->get('http://www.lynda.com/search?q=android');
unless ($response->is_success) {
die $response->status_line;
}
my $content = $response->decoded_content();
if (utf8::is_utf8($content)) {
binmode STDOUT,':utf8';
} else {
binmode STDOUT,':raw';
}

$content =~ s/[\h\v]+/ /g;
$content =~ s/\r|\n//g;
$content =~ s/<\/span>|<span>//g;

# Links
my @links = ($content =~ m/<a id="course-result-info.*href="(.*)\?.*class="title">/g);

# titles
my @titles = ($content =~ m/<a id="course-result-info.*class="title"> (.*)<\/a> <span class="author">/g);

print join(", ", @titles);
print "\n---------------------------\n";
print join(", ", @links);

但我只找到了最后一个匹配的(即 为 Amazon Kindle 设备开发应用程序)。

最佳答案

您的正则表达式解析已损坏,因为您使用的是贪婪匹配 .* 而不是非贪婪匹配 .*?

但是,更好的解决方案是根本不使用正则表达式,而是使用实际的 HTML 解析器,例如 Mojo::DOM具有 CSS 选择器的全部功能。

以下使用Mojo::UserAgentMojolicious 一起安装下载网页,然后使用 Mojo::DOM 解析返回的结果。在 Mojocast Episode 5 上有一个关于此框架的 8 分钟教程。 .

#!/usr/bin/perl

use strict;
use warnings;

use Mojo::UserAgent;

my $url = 'http://www.lynda.com/search?q=android';

my $dom = Mojo::UserAgent->new->get($url)->res->dom;

# Process all links
for my $link ($dom->find('a[id^="course-result-info"]')->each) {
printf "%s\n %s\n", $link->all_text(), $link->{href};
}

输出:

Android Studio First Look
http://www.lynda.com/Android-tutorials/Android-Studio-First-Look/143103-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Android SDK Essential Training
http://www.lynda.com/Android-tutorials/Android-SDK-Essential-Training/143102-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Distributing Android Apps
http://www.lynda.com/Android-tutorials/Distributing-Android-Apps/143101-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Building and Monetizing Game Apps for Android
http://www.lynda.com/Android-tutorials/Building-Monetizing-Game-Apps-Android/107169-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Building a Note-Taking App for Android
http://www.lynda.com/Android-tutorials/Building-Note-Taking-App-Android/122466-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Android SDK: Local Data Storage
http://www.lynda.com/Android-tutorials/Android-SDK-Local-Data-Storage/112584-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Android 4.1 SDK Jelly Bean New Features
http://www.lynda.com/Android-tutorials/Android-SDK-41-Jelly-Bean-New-Features/107922-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Building Mobile Apps with Google Maps Android API v2
http://www.lynda.com/Android-tutorials/Building-Mobile-Apps-Google-Maps-Android-API-v2/133347-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Building Android and iOS Applications with Flex
http://www.lynda.com/Flash-Builder-4-5-tutorials/Building-Android-and-iOS-Applications-with-Flex/80254-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Flash Professional CS5: Creating a Simple Game for Android Devices
http://www.lynda.com/Flash-CS5-tutorials/flash-professional-cs5-creating-a-simple-game-for-android-devices/74928-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Java Essential Training
http://www.lynda.com/Java-tutorials/Essential-Training/86005-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Up and Running with Java Applications
http://www.lynda.com/Android-tutorials/Up-Running-Java-Applications/94344-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Building Mobile Apps for Multiple Devices with Flash Professional
http://www.lynda.com/Flash-Professional-CS5-5-tutorials/Building-Mobile-Apps-for-Multiple-Devices-with-Flash-Professional/89049-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2
Developing Applications for Amazon Kindle Devices
http://www.lynda.com/Android-tutorials/Developing-Applications-Amazon-Kindle-Devices/117102-2.html?srchtrk=index%3a1%0alinktypeid%3a2%0aq%3aandroid%0apage%3a1%0as%3arelevance%0asa%3atrue%0aproducttypeid%3a2

关于regex - perl - 如何使用 RegEx 获取所有相似的匹配子字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24327487/

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