Reputation: 717
I'm doing the following to save the relevant portion of a URL to a Perl variable:
($url) = $url =~ m! (.+?\w) (?: /|\z) !x;
($url) = $url =~ /\/\/(.*)/;
I'm trying to save everything between "http(s|)://" and the next "/". Is there a better way to do this on a single line?
Upvotes: 0
Views: 46
Reputation: 35198
use URI
use URI;
my $uri = URI->new('http://www.stackoverflow.com/questions');
say $uri->host;
say $uri->host_port;
Outputs:
www.stackoverflow.com
www.stackoverflow.com:80
Upvotes: 4
Reputation: 385917
URI.pm is nice, but you don't always need the overhead.
my ($host_port) = $uri =~ m{ ^ https? :// ([^/]*) }xi;
Or if you just want the host,
my ($host) = $uri =~ m{ ^ https? :// ([^/:]*) }xi;
Upvotes: 1
Reputation: 14955
Use this regex: :\/\/([^\/]+)\/
Example:
#!/usr/bin/perl
$url = 'http://stackoverflow.com/questions/';
$url =~ /:\/\/([^\/]+)\//;
print $1."\n";
Upvotes: 1