What's the best way to parse URLs to extract the domain?

You can use domainatrix gem to get what you want: url. Domain + url. Public_suffix but you can just do some string manipulation like uri4..-1.

You can use domainatrix gem to get what you want: url. Domain + url. Public_suffix, but you can just do some string manipulation like uri4..-1.

This gem's pretty good. I guess I could then display the subdomains that! = 'www'.

– Marc Ripley Feb 27 at 22:21.

So while posting my own answer, I'm not saying that gems like domainatrix or public_suffix_server aren't good elegant solutions (although the latter bugged on me immediately which caused me to go this route). People suggesting using split() made me realize that I could just sub out '' if it existed and otherwise leave domains as they are without installing gems and using 1 simple line of code: domain = URI. Parse(url).host.

Sub(/\Awww\. /, '') This works with subdomains and multi-part suffixes (eg. Co.Uk).

Anybody see anything wrong with this? EDIT: Thanks sorens for pointing out the weak regex I was originally using. This expression is certainly better.

You should tighten up your regular expression. Www. Is going to match on any string that is www and any other character.

The period is a special regex character that means any single character. So if you were test against the site www2009. Org, you would end up removing "www2" and left with 009.org.At the very least, try \Awww\.

The "\A" will force the check to only consider it at the beginning of the string and the "\. " will escape the period to mean you are only looking for the period character. If you have trouble with regex, rubular.

Com is a good resource to help. – sorens Feb 28 at 0:35.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions