代码如下:
public static String getHost(String url) { Pattern p = Pattern.compile("(http://|https://)?([^/]*)",Pattern.CASE_INSENSITIVE); Matcher m = p.matcher(url); return m.find()?m.group(2):url; }
测试代码
System.out.println(getHost("http://www.baidu.com/#wd=%E6%88%91%E6%98%AF%E6%AD%8C%E6%89%8B&rsv_bp=0&tn=baidu&rsv_spt=3&ie=utf-8&rsv_sug3=11&rsv_sug4=643&rsv_sug1=11&inputT=5484&rsv_sug2=0"));
输出结果:
www.baidu.com
一句话:够简洁!
原文链接:https://www.f2er.com/regex/361744.html