in reply to Splitting an url in its components

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

Replies are listed 'Best First'.
Re^2: Splitting an url in its components
by massa (Hermit) on Jul 17, 2008 at 16:35 UTC
    (replying to self, duh)
    Here is a more complete regex:
    sub url_segments { local($_) = @_; my $segments = { url => $_ }; my @matches = m{ \A (?: ([a-z]{3,6}) # proto : (?: // )? )? (?: (?: ([a-z0-9_-]+) # user (?: : ([^@]+) ) @ # password )? ( [a-z0-9._-]+ ) # domain (?: : (\d+) )? # port ) (?: ( .*? / ) # location ( ( [^/]*? ) # filename_only ( ( \. \w*[a-z]\w* )* ) # ext ) (?: \? ( .* ) )? # query )? \Z }xi; return unless @matches; my @parts = qw(proto user pass domain port location filename filename_only ext query); $matches[$_] and $segments->{$parts[$_]} = $matches[$_] for 0 .. $#parts; $segments }
    []s, HTH, Massa
Re^2: Splitting an url in its components
by leocharre (Priest) on Jul 17, 2008 at 17:42 UTC
    Wow, that's pretty freaking elegant.
    I like this example more than the one after, because it's legible.
    Nice YAML touch- Thank you for showing us!
      At your service... :-)
      I like the first example better, too, but the second one gets more parts of the URI separated... and that's why I had to break it in so many lines! As for YAML, it's my favourite debugging tool (I think it's far more legible than Data::Dump(er)?)
      []s, HTH, Massa
        Its better if you use the official regex :)
        From the URI documentation:

        As an alternative to this module, the following (official) regular expression can be used to decode a URI:

        my($scheme, $authority, $path, $query, $fragment) = $uri =~ m|(?:([^:/?#]+):)?(?://([^/?#]*))?([^?#]*)(?:\?([^#]*))?(?:# +(.*))?|;