In Java, how do I extract the domain of a URL?
问题 I'm using Java 8. I want to extract the domain portion of a URL. Just in case I'm using the word "domain" incorrectly, what i want is if my server name is test.javabits.com I want to extract "javabits.com". Similarly, if my server name is firstpart.secondpart.lastpart.org I want to extract "lastpart.org". I tried the below final String domain = request.getServerName().replaceAll(".*\\.(?=.*\\.)", ""); but its not extracting the domain properly. Then I tried what this guy has in his site --