in reply to Re^4: Splitting an url in its components
in thread Splitting an url in its components

The POD for URI does not explain why this regex is "official"; in any case, it does not match correctly mailto: URIs. But my regexes did not count fragments, so they were not quite right either. New version follows:
sub url_segments { local($_) = @_; my $uri_re = qr{ (?: ([a-z]{3,6}) # proto : (?: // )? )? ( # authority (?: ([a-z0-9_-]+) # user (?: : ([^@]+) )? @ # password )? ( [a-z0-9._-]+ ) # domain (?: : (\d+) )? # port ) (?: ( # path ( .*? / ) # location ( # filename ( [^?#/]*? ) # filename_only ( (?: \. \w*[a-z]\w* )* ) # ext ) ) (?: \? ( [^#]* ) )? # query (?: \# ( .* ) )? # fragment )? }xi; my @matches = /\A($uri_re)\Z/; return unless @matches; my $segments; my @parts = qw(uri proto authority user pass domain port path location filename filename_only ext query frag); $matches[$_] and push @$segments, { $parts[$_] => $matches[$_] } for 0 .. $#parts; $segments }
Works quite reliably in preliminary tests :-)
Update: oops, a "?" was missing in the password line. fixed.
[]s, HTH, Massa