Question

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?

Was it helpful?

Solution 2

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;

OTHER TIPS

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

Use this regex: :\/\/([^\/]+)\/

Example:

#!/usr/bin/perl

$url = 'http://stackoverflow.com/questions/';
$url =~ /:\/\/([^\/]+)\//;
print $1."\n";
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top