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.
co
. \$\endgroup\$