//正则表达式一
String strContent="测试提取内容中的 http://www.baidu.com地址";
String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*";
//1、提取内容中http/https链接
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(strContent);
StringBuffer result = new StringBuffer();
if (matcher.find()) {
String url = matcher.group();
System.out.println(url);
}
/**正则表达式二
* 提取内容中http/https/短链链接,并且带有可点击标签
*/
String strContent="测试提取带有短链的内容 :i.douyin.com/t/DMGSYfo/短链地址";
String regex = "((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|([a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(strContent);
StringBuffer result = new StringBuffer();
if (matcher.find()) {
String url = matcher.group();
StringBuffer replace = new StringBuffer();
replace.append("<a href=\"").append(url);
replace.append("\" target=\"_blank\">"+url+"</a>");
matcher.appendReplacement(result, replace.toString());
}
matcher.appendTail(result);
System.out.println(result);
//http/https/短链正则:正则表达式二
//String regex = "((http[s]{0,1}|ftp)://[a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)|([a-zA-Z0-9\\.\\-]+\\.([a-zA-Z]{2,4})(:\\d+)?(/[a-zA-Z0-9\\.\\-~!@#$%^&*+?:_/=<>]*)?)";