您的位置:首页 > 编程语言 > Java开发

java判断文字中是否包含URL

2015-09-11 17:11 351 查看
package test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

public class Test {

public static void main(String[] args) {

String string="我是http://www.baidu.com";
//把中文替换为#
string = string.replaceAll("[\u4E00-\u9FA5]", "#");
System.out.println(string);
String url[]=string.split("#");
//转换为小写
if(url!=null&&url.length>0){
for(String tempurl:url){
if(StringUtils.isBlank(tempurl)){
continue;
}
tempurl = tempurl.toLowerCase();
String regex = "^((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,4})?" // 端口- :80
+ "((/?)|" // a slash isn't required if there is no file name
+ "(/[0-9a-z_!~*'().;?:@&=+$,%#-]+)+/?)$";

Pattern p = Pattern.compile(regex);
Matcher matcher = p.matcher(tempurl);
System.out.println(matcher.find());
}
}

}
}
参考:http://www.cnblogs.com/xmyy/articles/2871871.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: