Java中正則驗證URL是否合法



    public static boolean isURL(String str){
        //轉換為小寫
        str = str.toLowerCase();
        String regex = "^((https|http|ftp|rtsp|mms)?://)"  //https、http、ftp、rtsp、mms
                + "?(([0-9a-z_!~*'().&=+$%-]+: )?[0-9a-z_!~*'().&=+$%-]+@)?" //ftp的user@ 
               + "(([0-9]{1,3}\\.){3}[0-9]{1,3}" // IP形式的URL- 例如:199.194.52.184 
                 + "|" // 允許IP和DOMAIN(域名)
                 + "([0-9a-z_!~*'()-]+\\.)*" // 域名- www. 
                 + "([0-9a-z][0-9a-z-]{0,61})?[0-9a-z]\\." // 二級域名 
                + "[a-z]{2,6})" // first level domain- .com or .museum 
                + "(:[0-9]{1,5})?" // 端口號最大為65535,5位數
                + "((/?)|" // a slash isn't required if there is no file name 
                + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; 
        return  str.matches(regex);
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM