Perl Regex named capture variables

Simple example to extract, protocol, server and domain from a given URL:

my $test = "http:www.test.com";

$test =~ /^(?<protocol>.+)\:(?<server>.+)\.(?<domain>.+)$/;

print "protocol : ".$+{protocol}."\n";
print "Server   : ".$+{server}."\n";
print "Domain   : ".$+{domain}."\n";

Our Result:

sh-4.3$ perl main.pl 
protocol : http      
Server   : www.test 
Domain   : com

Leave a Reply

Your email address will not be published.

twelve + 16 =