2013-02-21 101 views
2

嘿我想在java中基於What is the best regular expression to check if a string is a valid URL?的url驗證,但由於某種原因,它不起作用。建議?在java中的URL驗證

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:[email protected]/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(); 
} 

} 
+1

@SotiriosDelimanolis - 實際上,它沒有。它只關心協議是否存在。 – 2013-02-21 18:27:33

+0

@BrianRoach好東西,謝謝。 – 2013-02-21 18:29:08

+1

個人而言,我不會爲了同樣的原因進行復雜的URL驗證,我不會進行復雜的電子郵件地址驗證。看看http://davidcel.is/blog/2012/09/06/stop-validating-email-addresses-with-regex/。 – entonio 2013-02-21 18:31:46

回答

2

正則表達式最初是用斜線包起來的(作爲PHP的PCRE所需的分隔符)。 Java不使用這些。

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

/i最後也是不受歡迎的。相反,寫

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

+1 - 最後還有一個'/'。 – 2013-02-21 18:30:25

1

您可以使用Apache 公地驗證 API。有一個類名爲UrlValidator,或類似的東西。
看看這個:http://commons.apache.org/validator/
我不瞭解很多正則表達式,所以我在這個主題上幫不了你。
祝你好運。

+1

雖然這確實完成了手頭的任務,除非你需要Apache公用程序中的其他東西,這是一個相當大的依賴關係,只是爲了驗證URL而附加到項目。 – 2013-02-21 18:28:51

+0

事實上,但你可以閱讀他們的代碼:) – 2013-02-21 18:30:47

+0

公平點,但大多數人,可悲的是,不會。 – 2013-02-21 18:33:54

0

雖然編寫正則表達式到URL驗證不是問題,爲什麼不只是使用java.io.URL類呢?只需創建URL的實例,如下所示:new URL(spec)如果語法錯誤,它將拋出MalformedURLExcption

+0

根據javadoc它只會拋出,如果協議丟失或'spec'爲'null' – 2013-02-21 18:31:16