The Java regular expression gets the host example of the url

  • 2020-04-01 03:00:34
  • OfStack


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;
 }


Related articles: