日期:2014-05-17 浏览次数:20727 次
<%@page import="java.util.regex.Pattern"%> <%@page import="java.util.regex.Matcher"%> ?? <%! //正规表达 private static final Pattern GREEDY_URL_MATCH_PATTERN = Pattern.compile("(http\\://|https\\://|www\\.){1}([^\\x00-\\x20()\"<>\\x7F-\\xFF])*(\\.com|\\.net|\\.cn|\\.org)", Pattern.CASE_INSENSITIVE); //URL取出 private List<String> findURL(Pattern pattern, String text){ List<String> list = new ArrayList<String>(); Matcher matcher = pattern.matcher(text); while(matcher.find()){ list.add(matcher.group()); } return list; } //获取转换Link后的字符串 public String convertToAnchorStr(String str) { List<String> urlLst = findURL(GREEDY_URL_MATCH_PATTERN, str); for (String url:urlLst) { String a = "<a href='" + url + "'>" + url + "</a>"; str = str.replace(url, a); } return str; } %> <% String text1 = "http://community.csdn.net dfsdfsadfsa www.google.com asdfsdfasdfsadf"; String text2 = "sssssssssssssssshttp://www.baidu.comsadf,sfsdafCSDN"; out.println(convertToAnchorStr(text1) + "</br>"); out.println(convertToAnchorStr(text2)); %>