public static String populateLinks(String str) { Matcher matcher = Pattern.compile("(https?|ftp)://[\\S]+").matcher(str); StringBuffer sb = new StringBuffer(); while(matcher.find()) { String link = matcher.group(); link = link.replaceAll("\\\\", "\\\\\\\\").replaceAll("\\$", "\\\\\\$"); matcher.appendReplacement(sb, String.format("<a href=\"%s\">%s</a>", link,link)); } matcher.appendTail(sb); return sb.toString(); }