tags:

views:

145

answers:

2

I have a ruby script that downloads URLs from an RSS server and then downloads the files at those URLs.

I need to split the URL into 2 components like so -

http://www.website.com/dir1/dir2/file.txt
-->   'www.website.com'    and    'dir1/dir2/file.txt'

I'm struggling to come up with a way to do this. I've been playing with regular expressions but nothing has worked. How would others go about doing this?

+9  A: 

Use the URI library.

require 'uri'
u = URI.parse("http://www.website.com/dir1/dir2/file.txt")
u.host
# => "www.website.com"
u.path
# => "/dir1/dir2/file.txt"
Simone Carletti
As a general rule: regular expressions are for parsing strings. URIs are not strings, they are URIs. Therefore, regular expressions should never be used to parse URIs. Ruby, just like pretty much every other language on the planet, already has a URI parsing library. Re-implementing that by hand is just plain stupid. (Well, actually, Ruby's URI is not standards-compliant, so re-implementing it actually *does* make sense, but somebody has already done that for us: http://Addressable.RubyForge.Org/ .)
Jörg W Mittag
A: 

In a simple way , you could use split .

split('/')[2]
TuxGeek