Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

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 = http://www.abc.com/def/ghi.html the result should be only http://www.abc.com

Comment on modify a URL
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: +//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 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.)

    URI

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
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? | Other CB clients
Other Users?
Others meditating upon the Monastery: (14)
As of 2015-07-07 13:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (88 votes), past polls