站长资讯网
最全最丰富的资讯网站

java如何判断是否是url

java如何判断是否是url

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),可以用来检查一个串是否含有某种子串、将匹配的子串替换或者从某个串中取出符合某个条件的子串等。

赞(0)
分享到: 更多 (0)

网站地图   沪ICP备18035694号-2    沪公网安备31011702889846号