Reputation: 191
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?
Upvotes: 0
Views: 444
Reputation: 336
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
Upvotes: 0
Reputation: 124265
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
Upvotes: 9