需求:
基于url获取host,case如下:
结果为snv.iteye.com
结果为snv.iteye.com
结果为snv.iteye.com
结果为“”
结果为“”
结果为“”
结果为“”
结果为www.iteye.com
www结果为www.iteye.com
结果为www.iteye.com
方法实现如下:
方法1:基于URI或者URL
依赖:
commons-lang commons-lang 2.6
代码:
private static String getHost(String url) { if (!(StringUtils.startsWithIgnoreCase(url, "http://") || StringUtils .startsWithIgnoreCase(url, "https://"))) { url = "http://" + url; } String returnVal = StringUtils.EMPTY; try { URI uri = new URI(url); returnVal = uri.getHost(); } catch (Exception e) { } if ((StringUtils.endsWithIgnoreCase(returnVal, ".html") || StringUtils .endsWithIgnoreCase(returnVal, ".htm"))) { returnVal = StringUtils.EMPTY; } return returnVal;}
方法2:基于正则
依赖:
commons-lang commons-lang 2.6
代码:
public static String getHost(String url) { if (!(StringUtils.startsWithIgnoreCase(url, "http://") || StringUtils .startsWithIgnoreCase(url, "https://"))) { url = "http://" + url; } String returnVal = StringUtils.EMPTY; try { Pattern p = Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+"); Matcher m = p.matcher(url); if (m.find()) { returnVal = m.group(); } } catch (Exception e) { } if ((StringUtils.endsWithIgnoreCase(returnVal, ".html") || StringUtils .endsWithIgnoreCase(returnVal, ".htm"))) { returnVal = StringUtils.EMPTY; } return returnVal;}