I used the following to extract the domain from a url: (They are test cases)
String regex = "^(ww[a-zA-Z0-9-]{0,}\\.)";
ArrayList<String> cases = new ArrayList<String>();
cases.add("www.google.com");
cases.add("ww.socialrating.it");
cases.add("www-01.hopperspot.com");
cases.add("wwwsupernatural-brasil.blogspot.com");
cases.add("xtop10.net");
cases.add("zoyanailpolish.blogspot.com");
for (String t : cases) {
String res = t.replaceAll(regex, "");
}
I can get the following results:
google.com
hopperspot.com
socialrating.it
blogspot.com
xtop10.net
zoyanailpolish.blogspot.com
The first four cases are good. The last one is not good. What I want is: blogspot.com
for the last one, but it gives zoyanailpolish.blogspot.com
. What am I doing wrong?
s/^[^.]+\.//
, then I suggest you do that. – tchrist Aug 27 '11 at 20:59example.com.tw
andexample.co.uk
? – BalusC Aug 27 '11 at 21:18co
(there are probably other ccTLDs you'd like to match). Grab the last two or three items depending on the outcome and join them together on the dot again. – BalusC Aug 27 '11 at 21:38