java正则表达式获取url的host示例

时间:2022-03-14 10:30:52
代码如下:

public static String getHost(String url){
  if(url==null||url.trim().equals("")){
   return "";
  }
  String host = "";
  Pattern p =  Pattern.compile("(?<=//|)((\\w)+\\.)+\\w+");
  Matcher matcher = p.matcher(url);  
  if(matcher.find()){
   host = matcher.group();  
  }
  return host;
 }