1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| public class DomainHelper {
public static final String REG_DOMAIN_FULL = "(?<=//)(.*?)((?=/)|(?=$))";
public static String getFullDomain(String url) { return getDomain(url, REG_DOMAIN_FULL); }
public static String getTopDomain(String url) { String fullDomain = getDomain(url, REG_DOMAIN_FULL); String[] split = fullDomain.split("\\."); String topDomain = null; if(split.length >= 2){ topDomain = split[split.length-2] + "."+split[split.length-1]; }else{ topDomain = fullDomain; } return topDomain; }
public static String getDomain(String url, String reg) { Pattern p = Pattern.compile(reg); Matcher m = p.matcher(url); String domain = null; while (m.find()) { domain = m.group(); } return domain; } }
|