Regex perl-如何使用正则表达式获得所有相似的匹配子字符串

Regex perl-如何使用正则表达式获得所有相似的匹配子字符串,regex,perl,Regex,Perl,我想解析一个网页(来自lynda.com)并获取所有课程的标题和链接。因此,我曾经获取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->

我想解析一个网页(来自lynda.com)并获取所有课程的标题和链接。因此,我曾经获取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);
#/usr/bin/perl
使用LWP::UserAgent;
严格使用;
使用警告;
my$ua=新LWP::UserAgent;
my$response=$ua->get('http://www.lynda.com/search?q=android');
除非($response->is\u success){
骰子$response->status\u行;
}
my$content=$response->decoded_content();
if(utf8::is_utf8($content)){
双模式标准输出':utf8';
}否则{
binmode标准输出':原始';
}
$content=~s/[\h\v]+//g;
$content=~s/\r |\n//g;
$content=~s/|//g;
#链接
我的@links=($content=~m//g);
#头衔

my@titles=($content=~m/因为您已经在手动解析HTML了,所以您可能希望在所有位置使用非贪婪量词,即
*?
而不是
*
,并使用
/s
修饰符,因为默认情况下
与换行符不匹配

my @links= $content =~ /<a id="course-result-info.*href="(.*?)\?.*?class="title">/sg;
my@links=$content=~//sg;
试试这些正则表达式:

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);
my@links=($content=~m/]*class=“title”>/g);
#头衔

my@titles=($content=~m/您的正则表达式解析被破坏,因为您使用的是贪婪匹配
*
而不是非贪婪的
*?

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

以下使用随安装的下载网页,然后使用
Mojo::DOM
解析返回的结果

产出:

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

谢谢@perreal我还添加了
my@links=($content=~m/回答很好,谢谢。但是如何使用
Mojo::UserAgent
从链接中删除查询字符串呢?
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