# String get domain name from web address

**URL:** <https://www.ruby-forum.com/t/string-get-domain-name-from-web-address/142573>\
**Category:** Ruby\
**Created:** [July 9, 2008, 10:56am UTC](https://www.ruby-forum.com/t/string-get-domain-name-from-web-address/142573 "2008-07-09T10:56:10Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 9, 2008, 10:56am UTC](https://www.ruby-forum.com/t/string-get-domain-name-from-web-address/142573/1 "2008-07-09T10:56:10Z")

</div>

Hi,

I could have a any web address, [www.yahoo.com](http://www.yahoo.com), [http://yahoo.com/](http://yahoo.com/),  
[http://www.yahoo.com/](http://www.yahoo.com/), [http://www.yahoo.co.uk/](http://www.yahoo.co.uk/) so i want i need is some  
ruby code that will always return the domain name “[yahoo.com](http://yahoo.com)”

Im not sure what the best way to this as the domain name isnt always in  
the same format.

Anyone got some ideas on how to get this as accuarte as possible??

JB

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 9, 2008, 1:34pm UTC](https://www.ruby-forum.com/t/string-get-domain-name-from-web-address/142573/2 "2008-07-09T13:34:39Z")

</div>

John B. wrote:

> Hi,
> 
> I could have a any web address, [www.yahoo.com](http://www.yahoo.com), [http://yahoo.com/](http://yahoo.com/),  
> [http://www.yahoo.com/](http://www.yahoo.com/), [http://www.yahoo.co.uk/](http://www.yahoo.co.uk/) so i want i need is some  
> ruby code that will always return the domain name “[yahoo.com](http://yahoo.com)”
> 
> Im not sure what the best way to this as the domain name isnt always in  
> the same format.
> 
> Anyone got some ideas on how to get this as accuarte as possible??
> 
> JB

For the examples you’ve shown, doing a regexp match on “[yahoo.co](http://yahoo.co)” would  
pick up all of them.

Note that [x.com](http://x.com) and [x.co.uk](http://x.co.uk) are two separate domains, and might be  
completely unconnected with each other. If you want to consider them as  
the same domain, it would be better to detect them separately, then map  
one to the other using a lookup table (Ruby hash).

Dave

---

<div class="post-metadata">

**Author:** ![johnnybutler7](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/8491ac/32.png) [@johnnybutler7](https://www.ruby-forum.com/u/johnnybutler7)\
**Post date:** [July 9, 2008, 4:43pm UTC](https://www.ruby-forum.com/t/string-get-domain-name-from-web-address/142573/3 "2008-07-09T16:43:04Z")

</div>

John B. wrote:

> Hi,
> 
> I could have a any web address, [www.yahoo.com](http://www.yahoo.com), [http://yahoo.com/](http://yahoo.com/),  
> [http://www.yahoo.com/](http://www.yahoo.com/), [http://www.yahoo.co.uk/](http://www.yahoo.co.uk/) so i want i need is some  
> ruby code that will always return the domain name “[yahoo.com](http://yahoo.com)”
> 
> Im not sure what the best way to this as the domain name isnt always in  
> the same format.
> 
> Anyone got some ideas on how to get this as accuarte as possible??
> 
> JB

First. You could have one bit that removes the http:// or [http://www](http://www).

Then, go to the first / and remove from there to the end of the string.

That would leave you with what you are seeking, I think.
