0

我试图从如下所示的日志文件中获取域名(http://www.example.com ):

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/  

我只想

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

有任何想法吗?

4

2 回答 2

9

怎么样

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

输出

http://maps.google.com
于 2013-05-31T13:59:45.250 回答
0

如果您不想自己处理,那么一个完整的证明方法如下:

URL url = new URL("http://l.macys.com/simi-valley-ca?cm_mmc=macys_");
    System.out.println(url.getProtocol() + "://" + url.getHost() + ((url.getPort()==-1)?"" : ":" + url.getPort()) + "/" );

如果您确定永远不会有端口类型的 url,您可以跳过 url.getPort!

干杯

于 2013-05-31T14:07:07.360 回答