Perl beginner with a question on regular expressions.
The code below successfully gets the webpage contents from my website.
Then, I check for a match to the pattern “search type: [Dir or Geo]”. That bit I just wrote is not the actual regex code, but text to show what I want to match.
Here’s an excerpt of what the get method actually captures (sorry, not enough reputation points yet to post images):
what: movers<br/>
where: toronto<br/>
search type:
Dir <br/>
between “search type:” and “Dir” there are tabs and spaces and that paragraph character you see in Word docs (right after the word “type:”.
Below is my code.
use strict;
use warnings;
use WWW::Mechanize;
my $searchtype = "nothing yet";
my $mech = WWW::Mechanize->new();
my $webpage;
$mech->credentials('user','password' );
foreach my $keyword qw(movers) {
print "\$keyword = $keyword\n";
my $url = "http://myurl";
$mech->get($url);
$webpage = $mech->content();
if ($webpage =~ /search type.+([A-Z][a-z][a-z])/) {
$searchtype = $1;
print "$searchtype\n";
}
}
So, why won’t my regular expression $webpage =~ /search type.+([A-Z][a-z][a-z])/ capture the “Dir” in the match variable $1?
Driving me nuts.
Louie
/./matches any character except newlines unless you use/./s. Since you want to match the newline, you’d have to add/s.But that would find the last three letters of the document. You actually want