java - java中的URL验证

标签 java regex validation url

嘿,我正在尝试基于 What is the best regular expression to check if a string is a valid URL? 进行网址验证在java中,但由于某种原因它不起作用。有建议吗?

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 不使用这些。

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

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

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

关于java - java中的URL验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15009573/

相关文章:

java - 在 fragment 中引用 MapView 时出现异常 - OSMdroid

java - 使用来自共享类加载器的 EHCache 时出现 ClassCastException

c# - 正则表达式之后仅接受 2 个位置

javascript - 如何使用 Javascript 或 JQuery 在 HTML 中突出显示输入文本字段的一部分

java - 带有预先决定的代码模板的新文件向导 eclipse 插件

java - 通过 hibernate 将类映射到数据库表时出现 'Entity class not found' 错误

regex - 在正则表达式替换中捕获字符串

正则表达式 : Remove line breaks if lookbehind shows a lowercase

javascript - jQuery 使用输入数组进行验证

c# - "Type ' http ://www. w3.org/2000/09/xmldsig# :SignatureType ' is not declared" in XmlDocument. 验证(...)