Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

modify a URL

by Anonymous Monk
on Nov 04, 2010 at 21:41 UTC ( #869563=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I need a simple regular expression to modify a url Ex: $url = the result should be only

Comment on modify a URL
Replies are listed 'Best First'.
Re: modify a URL
by ikegami (Pope) on Nov 04, 2010 at 22:33 UTC
    use URI qw( ); my $url = URI->new(...); my $base_url = $url->scheme . '://' . $url->host;

    It doesn't work for all schemes, but it does work for all schemes where your request makes sense. (http, https, ftp, for starters.)


Re: modify a URL
by Anonymous Monk on Nov 04, 2010 at 22:15 UTC
Re: modify a URL
by Your Mother (Chancellor) on Nov 04, 2010 at 22:19 UTC

    A way, with URI (this is not complete, fragments and queries must be dealt with too; read the docs to see how and write some tests to be sure it's behaving as you expect).

    perl -MURI -le '$u = URI->new(shift); $u->path(undef); print $u' http: +//
      $ perl -MURI -le"print URI->new_abs( '/' => URI->new( shift ))" http:/ +/
Re: modify a URL
by mjscott2702 (Pilgrim) on Nov 05, 2010 at 09:18 UTC
    If you want to do it with just a regex, could try something like:  perl -e '$url = ""; $url =~ s/(\w)\/.*$/\1/; print $url;'

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://869563]
Approved by planetscape
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2015-11-30 03:39 GMT
Find Nodes?
    Voting Booth?

    What would be the most significant thing to happen if a rope (or wire) tied the Earth and the Moon together?

    Results (757 votes), past polls