Get the second level domain of an URL (java)

前端 未结 10 893
长发绾君心
长发绾君心 2020-12-06 01:04

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 exam

10条回答
  •  攒了一身酷
    2020-12-06 01:08

    1.

    Method nonePublicDomainParts from simbo1905 contribution should be corrected because of TLD that contain ".", for example "com.ac":

    input: "com.abc.com.ac"

    output: "abc"

    correct output is "com.abc".

    To get SLD you may cut TLD from a given domain using method publicSuffix().

    2.

    A set should not be used because of domains that contain the same parts, for example:

    input: part1.part2.part1.TLD

    output: part1, part2

    correct output is: part1, part2, part1 or in the form part1.part2.part1

    So instead of Set use List.

提交回复
热议问题