java获取一段文字中的url地址并且http://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=467d91e3-dd10-480b-a322-71b65e66c736在网页中以链接http://forum.csdn.net/PointForum/Forum/PostTopic.aspx的形式显示发生的发生大幅飞洒放到http://forum.csdn.net/PointForum在网页显示如下:java获取一段文字中的url地址并且http://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=467d91e3-dd10-480b-a322-71b65e66c736在网页中以链接http://forum.csdn.net/PointForum/Forum/PostTopic.aspx的形式显示发生的发生大幅飞洒放到http://forum.csdn.net/PointForum
public static void main(String[] args) {
String s = "java获取一段文字中的url地址并且Http://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=467d91e3-dd10-480b-a322-71b65e66c736在网页中以链接http://forum.csdn.net/PointForum/Forum/PostTopic.aspx的形式显示发生的发生大幅飞洒放到http://forum.csdn.net/PointForum";
Matcher m = Pattern.compile("(?i)http://[^\u4e00-\u9fa5]+").matcher(s);
while(m.find()){
System.out.println(m.group());
}
}运行结果:
Http://forum.csdn.net/PointForum/Forum/PostTopic.aspx?forumID=467d91e3-dd10-480b-a322-71b65e66c736
http://forum.csdn.net/PointForum/Forum/PostTopic.aspx
http://forum.csdn.net/PointForum
public static void main(String[] args) {
String strContent="请到以下地址查询:查询地址:充值地址:https://pay.sdo.com/Index.aspx?type=card尊敬的用户: 您好,该卡帐号和密码是正确的,请到以下https://pay.sdo.com/Index.aspx?type=card 地址查询:查询地址:充值地址:https://pay.sdo.com/Index.aspx?type=card ";
String regex = "(http:|https:)//[^[A-Za-z0-9\\._\\?%&+\\-=/#]]*";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(strContent);
StringBuffer result = new StringBuffer();
while (matcher.find()) {
String urlStr=matcher.group();
StringBuffer replace = new StringBuffer();
replace.append("<a href=\"").append(urlStr);
replace.append("\" target=\"_blank\">"+urlStr+"</a>");
matcher.appendReplacement(result, replace.toString());
}
matcher.appendTail(result);
System.out.println(result);
}