java运用正则表达式推断是不是是url
/** * 推断一个字符串是不是为url * @param str String 字符串 * @return boolean 是不是为url * @author peng1 chen * **/ 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); }
正则表达式(regular expression)形貌了一种字符串婚配的形式(pattern),能够用来搜检一个串是不是含有某种子串、将婚配的子串替代或许从某个串中掏出相符某个前提的子串等。
更多java学问请关注java基础教程。
以上就是java怎样推断是不是是url的细致内容,更多请关注ki4网别的相干文章!