Search code examples
javadomain-name

In Java, how to delete everything after .com or .net


I am trying to get just the domain name (http://www.example.com) out of log files that looks like this:

http://maps.google.com/maps?hl=en&tab=wl
http://l.macys.com/simi-valley-ca?cm_mmc=macys_     
https://www.google.co.in/       
https://www.google.ca/  

I want just

http://maps.google.com/   
http://l.macys.com/    
https://www.google.co.in/   
https://www.google.ca/  

Any ideas?


Solution

  • How about

    URL url = new URL("http://maps.google.com/maps?hl=en&tab=wl");
    System.out.println(url.getProtocol()+"://"+url.getHost());
    

    Output

    http://maps.google.com