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:13
(Received via mailing list)
> I have that :).
>
> I'm trying to handle the case when I get a "URL" without the http://
>
> Wes

Instead of beating yourself up trying to come up with a regex, just
stick the 'http://' in front of it yourself.

unless path.index('http://', 0)
   path = 'http://' + path
   host = URI.parse(path).host
end

It's not necessarily the most robust solution, but you get the general
idea.

Regards,

Dan
A9c4658e9e475e13d790ae419acf01b6?d=identicon&s=25 Simon Kröger (Guest)
on 2006-04-17 20:57
(Received via mailing list)
Berger, Daniel wrote:
>> I have that :).
>>
>> I'm trying to handle the case when I get a "URL" without the http://
>>
>> Wes
>
> Instead of beating yourself up trying to come up with a regex, just
> stick the 'http://' in front of it yourself.

Definitely a solution, but the regexp shouldn't be that complicated too.

url.scan(%r{(?:.+://)?([^/]+)}).first.first

should do the trick. (?) well i hope thats not like the email-regexp...

cheers

Simon
7264fb16beeea92b89bb42023738259d?d=identicon&s=25 Christian Neukirchen (Guest)
on 2006-04-22 18:21
(Received via mailing list)
"Berger, Daniel" <Daniel.Berger@qwest.com> writes:

> unless path.index('http://', 0)
  unless path.index('http://') == 0
This topic is locked and can not be replied to.