java正则获取域名

public static void main(String[] args) {
		try{
			String url = "http://blog.csdn.net/xb12369";
			Pattern p = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",Pattern.CASE_INSENSITIVE);
			Matcher matcher = p.matcher(url);
			matcher.find();
			System.out.println("域名是:"+matcher.group());

			p = Pattern.compile("[^//]*?\\.(com|cn|net|org|biz|info|cc|tv)", Pattern.CASE_INSENSITIVE);
			matcher = p.matcher(url);
			matcher.find();
			System.out.println("完整的域名是:"+matcher.group());
		}catch(Exception ex){
			ex.printStackTrace();
		}
	}


控制台打印得到:

域名是:csdn.net
完整的域名是:blog.csdn.net

你可能感兴趣的:(域名获取)