cevallos.valtira
cevallos.valtira

Reputation: 191

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?

Upvotes: 0

Views: 444

Answers (2)

vineet
vineet

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

Pshemo
Pshemo

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

Related Questions