StringUtil.java

    public static final Pattern HTTP_OR_HTTPS_URL_PATTERN = Pattern.compile(
            "((http|https)://)" + //协议
                    "([\\w\\-]+\\.)*[\\w\\-]+" + //域名
                    "(:[0-9]+)?" + //端口
                    "((/[\\w-]*)*" + //路径
                    "(\\?[\\w-%:;&@#+=]*)?)"); //参数
    
    public static boolean isHttpOrHttpsUrl(String string) {
        Objects.requireNonNull(string);
        Matcher matcher = HTTP_OR_HTTPS_URL_PATTERN.matcher(string);
        return matcher.matches();
    }

    //全部打印true
    public static void main(String[] args) {
        System.out.println(isHttpOrHttpsUrl("http://localhost:8080?abcd=123&adfdsa=2212#abc"));
        System.out.println(isHttpOrHttpsUrl("http://localhost:8080?abcd=123"));
        System.out.println(isHttpOrHttpsUrl("http://localhost:8080"));
        System.out.println(isHttpOrHttpsUrl("http://localhost"));
        System.out.println(isHttpOrHttpsUrl("http://www.sina.com"));
        System.out.println(isHttpOrHttpsUrl("http://www.sina.com/abcd/adbc/?fileName=dasfj+@:;"));
    }
Logo

技术共进,成长同行——讯飞AI开发者社区

更多推荐