java中的URL验证 [英] URL validation in java

查看:31
本文介绍了java中的URL验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿,我正在尝试基于 检查字符串是否为有效 URL 的最佳正则表达式是什么? 在 java 中但由于某种原因它不起作用.建议?

Hey I'm trying a url validation based upon What is the best regular expression to check if a string is a valid URL? in java but for some reason it's not working. Suggestions?

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

public class urlValidate {

    /**
     * @param args
     */
    public static void main(String[] args) {
        test_url("http://brb/", false);
            test_url("https://localserver/projects/public/assets/javascript/widgets/UserBoxMenu/widget.css", false);
    test_url("https://www.google.com/", true);
    test_url("https://www.google.co.uk/projects/my%20folder/test.php", false);
    test_url("https://myserver.localdomain/", true);
    test_url("https://192.168.1.120/projects/index.php/", false);
    test_url("https://192.168.1.1/", true);
    test_url("https://projectpier-server.localdomain/projects/public/assets/javascript/widgets/UserBoxMenu/widget.css", false);
    test_url("https://2.4.168.19/project-pier?c=test&a=b", false);
    test_url("https://localhost/a/b/c/test.php?c=controller&arg1=20&arg2=20", false);
    test_url("https://user:password@localhost/a/b/c/test.php?c=controller&arg1=20&arg2=20", false);
    test_url("myserver",false);
    test_url("https://tomcat:8080/",true);
    test_url("https://facebook.com",false);
}

public static void test_url(String url, boolean expected) {
    boolean valid = isURLValid(url, true);
    String out = "URL Valid?: " + (valid ? "yes" : "no") + " for URL: "
            + url + ". Expected: " + (expected ? "yes" : "no") + ". ";
    if (valid == expected) {
        out += "PASS\n";
    } else {
        out += "FAIL\n";
    }
    System.out.println(out);
}

public static boolean isURLValid(String url, boolean forcehttps) {
    String regex = "";
    if (forcehttps) {
        regex = "/^(https):\\/\\/";
    } else {
        regex = "/^(https?):\\/\\/";
    }
    regex += "((([a-z0-9]\\.|[a-z0-9][a-z0-9-]*[a-z0-9]\\.)*"
            + "[a-z][a-z0-9-]*[a-z0-9]"
            + "|((\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])\\.){3}"
            + "(\\d|[1-9]\\d|1\\d{2}|2[0-4][0-9]|25[0-5])"
            + ")(:\\d+)?)"
            + "(#([a-z0-9$_\\.\\+!\\*\\'\\(\\),;:@&=-]|%[0-9a-f]{2})*)?(\\/)"
            + "$/i";

    Pattern p = Pattern.compile(regex);
    Matcher m = p.matcher(url); // get a matcher object
    return m.matches();
}

}

推荐答案

正则表达式最初用斜杠包裹(作为分隔符,这是 PHP 中 PCRE 所必需的).Java 不使用这些.

The regex is initially wrapped in slashes (to serve as delimiters, which are required for PCRE in PHP). Java does not use these.

if (forcehttps) {
    regex = "^(https):\\/\\";
} else {
    regex = "^(https?):\\/\\";
}

末尾的 /i 也是不可取的.相反,写

The /i at the end is also undesirable. Instead, write

Pattern p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE)

这篇关于java中的URL验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆