Forum: Ruby Re: Regular expression to parse out "host" part of URL strin

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F3b7b8756d0c7f71cc7460cc33aefaee?d=identicon&s=25 Berger, Daniel (Guest)
on 2006-04-17 18:00
(Received via mailing list)
> I am trying to write a regex to parse out the "host" part of
> a potential
> URL.

require 'uri'

URI.parse('http://www.cnn.com').host # www.cnn.com
URI.parse('http://www.cnn.com/').host # www.cnn.com
URI.parse('http://www.cnn.com/some/other/stuff').host # www.cnn.com

You'll have to prepend the 'http://' yourself.

Regards,

Dan
Bb4bdf2b184027bc38d4fb529770cde5?d=identicon&s=25 Wes Gamble (weyus)
on 2006-04-17 18:04
Daniel,

I have that :).

I'm trying to handle the case when I get a "URL" without the http://

Wes

Berger, Daniel wrote:
>> I am trying to write a regex to parse out the "host" part of
>> a potential
>> URL.
>
> require 'uri'
>
> URI.parse('http://www.cnn.com').host # www.cnn.com
> URI.parse('http://www.cnn.com/').host # www.cnn.com
> URI.parse('http://www.cnn.com/some/other/stuff').host # www.cnn.com
>
> You'll have to prepend the 'http://' yourself.
>
> Regards,
>
> Dan
This topic is locked and can not be replied to.