When you match your url against $RE{URI}{HTTP}{-keep}, what you want will be in $3, which you should extract immediately after the match. This sample code:
use strict; use warnings; use Regexp::Common qw(URI); my $URI = $RE{URI}{HTTP}{-keep}; my @uris = ( 'http://yahoo.co.uk/notfound.html', 'http://yahoo.co.uk:80/notfound.html?fn=john&ln=doe', 'http://yahoo.co.uk/', 'http://yahoo.co.uk', 'yahoo.co.uk/notfound.html', 'yahoo.co.uk', 'potato://yahoo.co.uk', ); ## Extract the host from the sample urls above for my $uri (@uris) { my ( $whole, $scheme, $host, $port, $path_q_slash, $path_q_noslash, $path_noq_noslash, $query, ) = ($uri =~ $URI); if (defined $whole) { printf "%-50s => %s\n", $whole, $host; } else { printf "%-50s => No match\n", $uri; } } ## ## An example with all the pieces: ## 'http://yahoo.co.uk:80/notfound.html?fn=john&ln=doe' ## my $uri = $uris[1]; my ($whole, @bits) = ($uri =~ $URI); printf "\n%s =>\n %s\n", $whole, (join qq(\n ), @bits ); __END__
prints:
http://yahoo.co.uk/notfound.html => yahoo.co.uk http://yahoo.co.uk:80/notfound.html?fn=john&ln=doe => yahoo.co.uk http://yahoo.co.uk/ => yahoo.co.uk http://yahoo.co.uk => yahoo.co.uk yahoo.co.uk/notfound.html => No match yahoo.co.uk => No match potato://yahoo.co.uk => No match http://yahoo.co.uk:80/notfound.html?fn=john&ln=doe => http yahoo.co.uk 80 /notfound.html?fn=john&ln=doe notfound.html?fn=john&ln=doe notfound.html fn=john&ln=doe
See Regexp::Common and Regexp::Common::URI::http.

Update: Added <readmore>


In reply to Re: Extracting a domain name from a url by fenLisesi
in thread Extracting a domain name from a url by abachus

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.