To do this, you need a list of valid domain names. Top level (.com, .org, etc.) And country codes (.us, .fr, etc.) Easy to find. Try http://www.icann.org/en/resources/registries/tlds .
For the second level (.co.uk, .org.au) you may need to find the code of each country in order to see its subdomains. Wikipedia is your friend.
Once you have the list, take the last two parts of the name that you have (google.com or co.uk) and see if it is on your second level list. If not, take the last part and see if it is on the top level list.
source share