字符串中获取网站地址(网站匹配)源码

来源:互联网 发布:淘宝满减后再退货 编辑:程序博客网 时间:2024/05/20 07:37


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


import edu.uci.ics.crawler4j.url.WebURL;


/**
 * Created by Avi Hayun on 9/22/2014.
 * Net related Utils
 *  crawler4j( https://github.com/yasserg/crawler4j)
 */
public class WebPage {

public static void main(String[] args) {
String url = "fsfsfs http:www.baidu.com" ;

Set<String> rs = WebPage.extractUrls(url) ;
System.out.println( rs );
}

    private static final Pattern pattern = initializePattern();


    public static Set<String> extractUrls(String input) {
        Set<String> extractedUrls = new HashSet<String>();


        if (input != null) {
            Matcher matcher = pattern.matcher(input);
            while (matcher.find()) {
                String urlStr = matcher.group();
                if (!urlStr.startsWith("http")) {
                    urlStr = "http://" + urlStr;
                }


                extractedUrls.add(urlStr);
            }
        }


        return extractedUrls;
    }


    /** Singleton like one time call to initialize the Pattern */
    private static Pattern initializePattern() {
        return Pattern.compile("\\b(((ht|f)tp(s?)\\:\\/\\/|~\\/|\\/)|www.)" +
                               "(\\w+:\\w+@)?(([-\\w]+\\.)+(com|org|net|gov" +
                               "|mil|biz|info|mobi|name|aero|jobs|museum" +
                               "|travel|[a-z]{2}))(:[\\d]{1,5})?" +
                               "(((\\/([-\\w~!$+|.,=]|%[a-f\\d]{2})+)+|\\/)+|\\?|#)?" +
                               "((\\?([-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
                               "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)" +
                               "(&(?:[-\\w~!$+|.,*:]|%[a-f\\d{2}])+=?" +
                               "([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)*)*" +
                               "(#([-\\w~!$+|.,*:=]|%[a-f\\d]{2})*)?\\b");
    }
}
原创粉丝点击