Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

modify a URL

by Anonymous Monk
on Nov 04, 2010 at 21:41 UTC ( [id://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 = http://www.abc.com/def/ghi.html the result should be only http://www.abc.com

Replies are listed 'Best First'.
Re: modify a URL
by ikegami (Patriarch) 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.)

    URI

Re: modify a URL
by Anonymous Monk on Nov 04, 2010 at 22:15 UTC
Re: modify a URL
by Your Mother (Archbishop) 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: +//www.abc.com/def/ghi.html http://www.abc.com
      $ perl -MURI -le"print URI->new_abs( '/' => URI->new( shift ))" http:/ +/www.abc.com/def/ghi.html?s=d http://www.abc.com/
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 = "http://www.abc.com/def/ghi.html"; $url =~ s/(\w)\/.*$/\1/; print $url;'

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2024-03-19 04:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found