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

**URL:** <https://www.ruby-forum.com/t/re-regular-expression-to-parse-out-host-part-of-url-strin/93986>\
**Category:** Ruby\
**Created:** [April 17, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/re-regular-expression-to-parse-out-host-part-of-url-strin/93986 "2006-04-17T18:13:11Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 17, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/re-regular-expression-to-parse-out-host-part-of-url-strin/93986/1 "2006-04-17T18:13:11Z")

</div>

> 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

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 17, 2006, 8:57pm UTC](https://www.ruby-forum.com/t/re-regular-expression-to-parse-out-host-part-of-url-strin/93986/2 "2006-04-17T20:57:34Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![BergerS\_Daniel](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/bc8723/32.png) [@BergerS\_Daniel](https://www.ruby-forum.com/u/BergerS_Daniel)\
**Post date:** [April 22, 2006, 6:21pm UTC](https://www.ruby-forum.com/t/re-regular-expression-to-parse-out-host-part-of-url-strin/93986/3 "2006-04-22T18:21:19Z")

</div>

“Berger, Daniel” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

> unless path.index(‘http://’, 0)  
> unless path.index(‘http://’) == 0
