문제

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