Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

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

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 (Bishop) 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 all is quiet...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (2)
As of 2018-04-21 04:27 GMT
Find Nodes?
    Voting Booth?