All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.eshore.tools.URLutils Maven / Gradle / Ivy

There is a newer version: 2.0.3
Show newest version
package com.eshore.tools;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class URLutils {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(getHostFromUrl("http://snv.iteye.com/blog/1992991"));
		System.out.println(getHostFromUrl("https://snv.iteye.com/blog/1992991"));
		System.out.println(getHostFromUrl("http://snv.iteye.com:8080/blog/1992991"));
		System.out.println(getHostFromUrl("http://snv.iteye.com"));
		System.out.println(getHostFromUrl("http://snv.iteye.com/"));
		System.out.println(getHostFromUrl("http://snv.iteye.com:8999?qweqwe"));
		System.out.println(getHostFromUrl("https://snv.iteye.com:8999/"));
	}
	private static Pattern p = Pattern.compile("http[s]?://([^/:]*).*");
	
	/**
	 * 从URL中获取host
	 *
	 * @param url URL
	 * @return HOST
	 */
	public static String getHostFromUrl(String url) {
		if(url==null)return null;
		if(!url.startsWith("http"))return null;
		Matcher m=p.matcher(url);
		if(m.matches()) {
			return m.group(1);
		}
		return null;
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy