2007/1/4, M. Lewis <[EMAIL PROTECTED]>:


I'm trying to parse the domain name out of some URLs. In the example
data, my regex works fine on the first two URLs, but clips off the first
two characters of the domain on the third example. My regex probably
could be much better.

#!/usr/bin/perl

use strict;
use warnings;

my $regex = qr'http://\w+?\.?\w+?\.?(\w+\.com)';

while(<DATA>){
   if (/$regex/o){print "$1 \t $_"}
}

__DATA__
http://www.asldkjlkwerj.com/
http://w71r2xk22q1affwp1ewpjeee.alaskjhhawe.com/?
http://qwlkjekwl.com/?IJESRKUFZedFRCVFJYQV4cUFtY

Thanks for any pointers,
Mike


Why don't you use the URI module for that?

<code>

use strict;
use warnings;
use URI;

while (<DATA>) {
   my $uri = URI->new($_);
   print $uri->host, "\n";
}
</code>

HTH.
--
Igor Sutton Lopes <[EMAIL PROTECTED]>

Reply via email to