Gossamer Forum
Home : General : Perl Programming :

Regex Question...

Quote Reply
Regex Question...
Hi. How would you go about trying to match a word in the middle of a variable? E.g.

$line = "something ace-installer.com something else you get he idea";
if ($line =~ /.+ace-installer\.com.+/) { &sendit; }

I know that .+ should match stuff wherever it is after something, but it doesn't seem to be matching Frown

Any ideas?

Thanks

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Regex Question... In reply to
You only need...

Code:
$line = "something ace-installer.com something else you get the idea";
&sendit if ($line =~ /ace-installer\.com/);
Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
=~

Wil

Quote Reply
Re: Regex Question... In reply to
Pardon wil?

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
Ok, now I'm using;

if ($donation eq "No") {

$uri = "$linkpage";
@page = get($uri) || die &error("Unable to connect to the page you said was the 'link back page'. Please double check and try again.");


foreach $line (@page)
{

if ($line =~ /ace-installer/) { &sendit; exit; }
}

&error("Unable to find a recipricle link on the page you provided. The link must be there before we will do the install!");

}

The idea is that it goes through each line, and if there is still no match by the end it will goto the error sub. However, it still isn't working. The error is being shown every time Frown

I know the pages HTML is being passed through to the array, as I tried to print each $line to see if it was. It just doesn't seem to be matching Frown Anyone got any other ideas?

Thanks

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Regex Question... In reply to
Code:
if ($donation eq "No") {
$uri = $linkpage; # No quotations needed.
@page = get($uri) || die &error("Unable to connect."); # Make sure you have an exit in sub error
}
Code:
my $found;
foreach (@page) {
if (/ace-installer/) {
$found = 1;
last;
} else {
$found = 0;
}
}
Code:
if ($found == 1) {
&sendit; # No exit needed
} else {
&error("Unable to find a recipricle link on the page you provided. The link must be there before we will do the install!");
}
That should work.



Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
You may be better using something like......

Code:
#!/usr/bin/perl
Code:
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;
Code:
my ($url,$agent,$page,$results,$bs,@ahref);
$url = $linkpage;
$agent = new LWP::UserAgent;
Code:
my @links = ();
$page = HTML::LinkExtor->new(\&scan);
$results = $agent->request(HTTP::Request->new(GET => $url), sub {$page->parse($_[0])});
my $bs = $results->base;
@links = map { $_ = url($_, $bs)->abs; } @links;
Code:
sub scan {
my($html, %tag) = @_;
return if $html ne 'a';
push(@links, values %tag);
}
Code:
if (grep { /ace-installer\.com/ } @links) {
&sendit;
} else {
&error("No found");
}
I've not tested that but it has a 90% chance of working.

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
Thanks for that Paul. It works now Smile

Andy

webmaster@ace-installer.com
http://www.ace-installer.com
Quote Reply
Re: Regex Question... In reply to
Paul

I was just stating that he could us string matching to achieve his objective.

Wil

Quote Reply
Re: Regex Question... In reply to
No worries.

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
Could you give an example wil...(out of interest).

The code I gave is already matching a string.

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
$string =~ /match_this/gi;

?

Wil

Quote Reply
Re: Regex Question... In reply to
Yeah...that is exactly the same as what I said...

Code:
$line =~ /ace-installer\.com/
...except I added &sendit as that is what Andy wanted it to do.

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
I just added an i into the code to ignore case.

I was not questioning your code. I was just making a suggestion.

Chill! Shocked

Wil

Quote Reply
Re: Regex Question... In reply to
Sorry you misunderstood...I am chilled...I just wasn't sure what you meant when you just typed =~ because you seemed to be suggesting the same method as I already did...no biggie.

You don't need the g as it only needs to match once.

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
Yeah, I put the g in there because I altered the code from a string translation & substitution I had.

I'm not sure if the g even works in pattern matching?

Wil

Quote Reply
Re: Regex Question... In reply to
I'm not sure, but I can't think of an occassion when it is even needed...only in substitution etc...

If you needed to match more than one occurance of something in a string you could use tr///;

eg...

Code:
my $count = ($string =~ tr/word//);
($count > 1) ? 'Yes' : 'No';

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
Hmm. I just thought that Perl version 5 used g instead of the older format? Who knows? Both work.

Anyway, another day another dollar.

Wil

Quote Reply
Re: Regex Question... In reply to
Speak for yourself....another day another university assignment....Frown

Mods:http://wiredon.net/gt/download.shtml
Installations:http://wiredon.net/gt/
Quote Reply
Re: Regex Question... In reply to
In Reply To:
I'm not sure, but I can't think of an occassion when it is even needed...only in substitution etc...
The GT::Template module (actually, this part of the code moved to GT::Template::Parser last week, but that hasn't yet been distributed with anything) uses g with a regular expression:

Code:
while ($TPL =~ /($begin\s*(--.*?--|.+?)\s*$end)/gs) {
...
}
Basically, this loops through every match in the string.

In Reply To:
If you needed to match more than one occurance of something in a string you could use tr///;

eg...

Code:
my $count = ($string =~ tr/word//);
($count > 1) ? 'Yes' : 'No';
I think you should be using s/word//g there instead of tr/word//, unless you intended for "do" to return "Yes" :)

Jason Rhinelander
Gossamer Threads
jason@gossamer-threads.com