Extract name of website from an absolute URL [closed]

You will take input such as

http://examples.example.co.nz/abcdefg

and return the name of the website, in which this example would be example.

To be more specific, I am looking for the Second Level Domain, which immediately precedes the TLD (Top Level Domain), but the Third Level Domain if the website is a Country Code Second-Level Domain.

Shortest bytes wins (competition ends March the 5th)!

closed as unclear what you're asking by Howard, Iszi, Jonathan Van Matre, Toothbrush, The Guy with The HatMar 9 '14 at 13:41

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• Edited to make it more clear what you are looking for. Still, the end result of this approach to getting the site name is going to be pretty poor in many cases. Del.icio.us, Blo.gs, Instagr.am. Even stats.stackexchange.com, in which the third-level domain is the most significant identifier, but even then it is not the title of the site, which is Cross Validated. – Jonathan Van Matre Mar 1 '14 at 5:35
• In your example, the SLD is co. – Peter Taylor Mar 1 '14 at 13:10
• Aha. The question could still use improvement: firstly to define an authoritative source on which SLDs are ccSLDs, and secondly to define behaviour when the input isn't in that exact format (e.g. may we assume that the URL is always http? May we assume that it's an absolute URL? May we assume that the domain is never a TLD? May we assume that it never includes a username, password, or port number component? – Peter Taylor Mar 1 '14 at 15:45
• I've reviewed this question 4 times! enough closing and reopening! – TheDoctor Mar 1 '14 at 21:25
• I don't think the edits address any of my five requests for clarification. – Peter Taylor Mar 3 '14 at 11:51

Ruby, 15 characters

Assuming input is stored in variable s.

s.split(?.)[-2]


GolfScript, 7

Exact translation.

"."/-2=


Ruby with edge cases, 28

Such as http://example.com/foo.bar

s.split(?/)[2].split(?.)[-2]


GolfScript with edge cases, 13

"/"/2="."/-2=

• @TheDoc That's no reason to downvote. Should I downvote all Java answers just because I don't like that language? You're still free to answer in whatever language you want; there's nothing stopping you. – Doorknob Mar 1 '14 at 4:51
• @TheDoc That's pretty bad sportsmanship, downvoting all answers that beat yours. There's notHing stopping you from answering in Python. – Doorknob Mar 1 '14 at 4:55
• @TheDoc Please do not vote on the language used in a post... – Doorknob Mar 1 '14 at 5:14
• @Doorknob Unless the post is full of foul language. ;-) – Justin Mar 1 '14 at 6:31
• @TheDoc Yes, I downvote posts that have arbitrary and unnecessary restrictions. There is absolutely no reason to disallow a language. – Doorknob Mar 1 '14 at 14:27

JavaScript, 27 bytes

Assuming a is the variable and c is a 1 or 0,

i=a.split('.')[i.length-c];


would work. c is set to 3 if the domain is a ccsld and set to 2 if the domain isn't a ccsld. c can also be edited to a larger number if the page has dots in it.

• Uhhh, x==false?y:z is the exact same as x?z:y. – Doorknob Mar 1 '14 at 5:02
• Ummm, I'm not sure what you're saying... – Doorknob Mar 1 '14 at 5:03
• ...when did I say that? Read my comments carefully... – Doorknob Mar 1 '14 at 5:05
• @Doorknob Oh, yes... :) – Cilan Mar 1 '14 at 5:05
• What about i[i.length-ccsld-2]? – grc Mar 1 '14 at 7:05

C, 120

char s[999],*p,*q,*r;main(){gets(s);for(q=p=strstr(s,"//")+2,*strstr(p,"/")=0;*p;p++)if(*p=='.')*p=0,r=q,q=p+1;puts(r);}


The 2 "solutions' above fail, what if the website has the URL www.some1.name.ext/whatever.this/www/?

The solution in JavaScript is:

var arr = myurl.split('/');
var domain = arr[2];
var arr2 = domain.split('.');
var name = arr2[arr2.length-2];


or

name = myurl.split('/')[2].split('.')[myurl.split('/')[2].split('.').length-2];

• Please golf your code and state the length (remove redundant space, use one-letter variable names...). – Hosch250 Mar 1 '14 at 5:48

Python - 30

Python's not too good for these things.

a.split("/")[2].split(".")[-2]


Doesn't work for TLDs with more than one ..

• This doesn't work with the listed example - returns 'co' instead of 'example'. – DarkHeart Mar 3 '14 at 11:36
• @user17752 Ah - I guess it only works for general TLDs with one .. Since there can be an arbitrary number of .s in a URL, how can this work? Especially with the new custom TLDs? – cjfaure Mar 3 '14 at 12:36

Python2 172, Python3 156

This one is long, but i think it fullfills all the requeriments of the question.

s = "http://examples.example.co.nz/abcdefg"

#BEGIN_CODE
import re,urllib2
m=re.search(r"(?:\w+\.)*(\w+)\.(\w+)\.(\w+)",s)
g=m.groups()
r=g[0]if g[2]in l else g[1]
#END_CODE

print(r)


l contains a list of all country codes from wikipedia
m contains the last third, second and first level domains, dropping all the others
r contains the result

All could go in a line, but since ; is the same as \n i leave it like this.

EDIT: Tested with www.some1.name.ext/whatever.this/www/? and returns name so i guess it works.

Python 3 version

import re,requests as u
g=re.search(r"(?:\w+\.)*(\w+)"+"\.(\w+)"*2,s).groups()
r=g[0]if g[2]in re.findall("..",u.get("http://goo.gl/molpki").text) else g[1]


Same as above, r contains result.