If you _really_ want to reinvent the wheel, you can do with a single regex like:
use strict; use warnings; sub url_segments { local($_) = @_; my $segments = { url => $_ }; return unless m{ \A (?: ([a-z]{3,6}) : (?://)? )? # proto ( [:a-z0-9@._-]+ ) # domain (?: ( .*? / ) # location ( ( [^/]*? ) # filename_only ( (?: \. \w*[a-z]\w* )* ) # ext ) (?: ( \? .* ) )? # query )? \Z }x; $segments->{proto} = $1 if $1; $segments->{domain} = $2 if $2; $segments->{location} = $3 if $3; $segments->{filename} = $4 if $4; $segments->{filename_only} = $5 if $5; $segments->{ext} = $6 if $6; $segments->{query} = $7 if $7; $segments } use YAML; print Dump( { map { $_ => url_segments $_ } qw( http://foo.com/downloads/pkg-5.6.tar.gz http://bar.com/list/file-5.6-win32.zip example.org/subd/golf.txt http://digg.com/page4 http://digg.com/page4?format=printable http://www.cnn.com/2008/US/07/17/beck.che.guevara/index.html https://tomwtriker:imzadi@coldmail.net/goethe.3.5.tar mailto:tomwtriker@coldmail.net ) } )
Result:
--- example.org/subd/golf.txt: domain: example.org ext: .txt filename: golf.txt filename_only: golf location: /subd/ url: example.org/subd/golf.txt http://bar.com/list/file-5.6-win32.zip: domain: bar.com ext: .zip filename: file-5.6-win32.zip filename_only: file-5.6-win32 location: /list/ proto: http url: http://bar.com/list/file-5.6-win32.zip http://digg.com/page4: domain: digg.com filename: page4 filename_only: page4 location: / proto: http url: http://digg.com/page4 http://digg.com/page4?format=printable: domain: digg.com filename: page4 filename_only: page4 location: / proto: http query: '?format=printable' url: http://digg.com/page4?format=printable http://foo.com/downloads/pkg-5.6.tar.gz: domain: foo.com ext: .tar.gz filename: pkg-5.6.tar.gz filename_only: pkg-5.6 location: /downloads/ proto: http url: http://foo.com/downloads/pkg-5.6.tar.gz http://www.cnn.com/2008/US/07/17/beck.che.guevara/index.html: domain: www.cnn.com ext: .html filename: index.html filename_only: index location: /2008/US/07/17/beck.che.guevara/ proto: http url: http://www.cnn.com/2008/US/07/17/beck.che.guevara/index.html https://tomwtriker:imzadi@coldmail.net/goethe.3.5.tar: domain: tomwtriker:imzadi@coldmail.net ext: .tar filename: goethe.3.5.tar filename_only: goethe.3.5 location: / proto: https url: https://tomwtriker:imzadi@coldmail.net/goethe.3.5.tar mailto:tomwtriker@coldmail.net: domain: tomwtriker@coldmail.net proto: mailto url: mailto:tomwtriker@coldmail.net
[]s, HTH, Massa

In reply to Re: Splitting an url in its components by massa
in thread Splitting an url in its components by baurel

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.