Java tutorial
//package com.java2s; /* * This file is part of the Droid Roach project * Copyright 2013 Stefano Gabryel * Author: Stefano Gabryel * License: GPL v3 - http://www.gnu.org/licenses/gpl-3.0.html * Author website: http://www.stefano-workshop.com * Project website: http://droid-roach.stefano-workshop.com * */ import java.net.URI; import java.util.regex.Matcher; import java.util.regex.Pattern; public class Main { public static String getDomain(String target) { Pattern p = Pattern.compile(".*?([^.]+\\.[^.]+)"); URI uri; try { uri = new URI(target); } catch (Exception e) { return "http"; } String host = uri.getHost(); Matcher m = p.matcher(host); if (m.matches()) return m.group(1); else return host; } }