Richard H Richard H - 11 months ago 79
Java Question

Get the second level domain of an URL (java)

I am wondering if there is a parser or library in java for extracting the second level domain (SLD) in an URL - or failing that an algo or regex for doing the same. For example:

URI uri = new URI("");

String host = uri.getHost();


which prints:

Now what I'd like to do is robustly identify the SLD ("") component. Any ideas?

Edit: I'm ideally looking for a general solution, so I'd match ".uk" in "", "" in "" and ".com" in "".


Answer Source

Don't know your purpose but Second-Level Domain may not mean much to you. You probably need to find public suffix and the domain right below it is what you are looking for.

Apache Http Component (HttpClient 4) comes with classes to handle this,


You need to download the public suffix list from here,