-8
\$\begingroup\$

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)!

\$\endgroup\$
12
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 1, 2014 at 5:35
  • 3
    \$\begingroup\$ In your example, the SLD is co. \$\endgroup\$ Commented Mar 1, 2014 at 13:10
  • 4
    \$\begingroup\$ 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? \$\endgroup\$ Commented Mar 1, 2014 at 15:45
  • 1
    \$\begingroup\$ I've reviewed this question 4 times! enough closing and reopening! \$\endgroup\$
    – TheDoctor
    Commented Mar 1, 2014 at 21:25
  • 3
    \$\begingroup\$ I don't think the edits address any of my five requests for clarification. \$\endgroup\$ Commented Mar 3, 2014 at 11:51

6 Answers 6

8
\$\begingroup\$

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=
\$\endgroup\$
11
  • 4
    \$\begingroup\$ @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. \$\endgroup\$
    – Doorknob
    Commented Mar 1, 2014 at 4:51
  • 7
    \$\begingroup\$ @TheDoc That's pretty bad sportsmanship, downvoting all answers that beat yours. There's notHing stopping you from answering in Python. \$\endgroup\$
    – Doorknob
    Commented Mar 1, 2014 at 4:55
  • 4
    \$\begingroup\$ @TheDoc Please do not vote on the language used in a post... \$\endgroup\$
    – Doorknob
    Commented Mar 1, 2014 at 5:14
  • 5
    \$\begingroup\$ @Doorknob Unless the post is full of foul language. ;-) \$\endgroup\$
    – Justin
    Commented Mar 1, 2014 at 6:31
  • 5
    \$\begingroup\$ @TheDoc Yes, I downvote posts that have arbitrary and unnecessary restrictions. There is absolutely no reason to disallow a language. \$\endgroup\$
    – Doorknob
    Commented Mar 1, 2014 at 14:27
4
\$\begingroup\$

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.

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

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);}
\$\endgroup\$
2
\$\begingroup\$

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];
\$\endgroup\$
1
  • \$\begingroup\$ Please golf your code and state the length (remove redundant space, use one-letter variable names...). \$\endgroup\$
    – user10766
    Commented Mar 1, 2014 at 5:48
2
\$\begingroup\$

Python - 30

Python's not too good for these things.

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

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

\$\endgroup\$
2
  • \$\begingroup\$ This doesn't work with the listed example - returns 'co' instead of 'example'. \$\endgroup\$
    – user17752
    Commented Mar 3, 2014 at 11:36
  • \$\begingroup\$ @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? \$\endgroup\$
    – cjfaure
    Commented Mar 3, 2014 at 12:36
1
\$\begingroup\$

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
l=re.findall("..",urllib2.urlopen("http://goo.gl/molpki").read())
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.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.