Extract hostname name from string

后端 未结 28 2074
情歌与酒
情歌与酒 2020-11-22 07:15

I would like to match just the root of a URL and not the whole URL from a text string. Given:

http://www.youtube.co         


        
28条回答
  •  说谎
    说谎 (楼主)
    2020-11-22 08:18

    Try below code for exact domain name using regex,

    String line = "http://www.youtube.com/watch?v=ClkQA2Lb_iE";

      String pattern3="([\\w\\W]\\.)+(.*)?(\\.[\\w]+)";
    
      Pattern r = Pattern.compile(pattern3);
    
    
      Matcher m = r.matcher(line);
      if (m.find( )) {
    
        System.out.println("Found value: " + m.group(2) );
      } else {
         System.out.println("NO MATCH");
      }
    

提交回复
热议问题