java使用正則表達(dá)式判斷是否是url
/** * 判斷一個(gè)字符串是否為url * @param str String 字符串 * @return boolean 是否為url * @author peng1 chen * **/ public static boolean isURL(String str){ //轉(zhuǎn)換為小寫 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]\." // 二級(jí)域名 + "[a-z]{2,6})" // first level domain- .com or .museum + "(:[0-9]{1,5})?" // 端口號(hào)最大為65535,5位數(shù) + "((/?)|" // a slash isn't required if there is no file name + "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$"; return str.matches(regex); }
正則表達(dá)式(regular expression)描述了一種字符串匹配的模式(pattern),可以用來檢查一個(gè)串是否含有某種子串、將匹配的子串替換或者從某個(gè)串中取出符合某個(gè)條件的子串等。