Вопрос

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?

Это было полезно?

Решение

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

Другие советы

If you don't want to handle it yourself, then a full proof way is following:

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()) + "/" );

You can skip url.getPort if you are sure that there will never be a port type url!!

Cheers

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top