日期:2014-05-20 浏览次数:21105 次
public static void main(String[] args) { String reg="@.*?(?=[.,:;!?\\s#@。,:;!?])"; Pattern pattern=Pattern.compile(reg); System.out.println(pattern.toString()); Matcher matcher=pattern.matcher("fsafaf加大@梵净山,金佛山@就佛网 杰弗里斯abc@sina.com fsla"); StringBuffer sb=new StringBuffer(); while(matcher.find()){ System.out.println(matcher.group()); matcher.appendReplacement(sb, "<a href='abc'>"+matcher.group()+"</a>"); } matcher.appendTail(sb); System.out.println(sb); }
public static void main(String[] args) { String reg = "([a-zA-Z0-9-.]*)@(.*?)([,:;!?\\s#@。,:;!?]|$|\\.(?:com|org|net))"; Pattern pattern = Pattern.compile(reg); System.out.println(pattern.toString()); Matcher matcher = pattern .matcher("fsafaf加大@梵净山,金佛山@就佛网 杰弗里斯abc@sina.com fsla;@sina.com;ssd@sina"); StringBuffer sb = new StringBuffer(); while (matcher.find()) { String begin = matcher.group(1); String middle = matcher.group(2); String end = matcher.group(3); // 判断email if (!begin.equals("")) { boolean isEmail = middle.length() != 0; for (int i = 0; i < middle.length(); i++) { if (!Character.isLetterOrDigit(middle.charAt(i))) { isEmail = false; break; } } if (isEmail) { if (end.length() > 1) { //确认是email continue; } } } matcher.appendReplacement(sb, begin + "<a href='abc'>" + "@" + middle + "</a>" + end); } matcher.appendTail(sb); System.out.println(sb); }