Java で URL 情報からドメインやらを抽出してみたかったので,Guava
の InternetDomainName
を使ってみた.
InternetDomainName (Guava: Google Core Libraries for Java 19.0-SNAPSHOT API)
サンプルコード
package com.kakakakakku.guava; import com.google.common.net.InternetDomainName; import java.net.URL; public class UrlParser { public static void main(String[] args) throws Exception { URL url = new URL("http://kakakakakku.hatenablog.com/"); System.out.println(url.getHost()); System.out.println(InternetDomainName.from(url.getHost()).topPrivateDomain()); System.out.println(InternetDomainName.from(url.getHost()).publicSuffix()); } }
実行結果
kakakakakku.hatenablog.com hatenablog.com com