代码如下:
public class RegTest {
public static void main(String[] args) {
Pattern r = Pattern.compile(
"\\b \n"+
"#把捕获的地址保存到$1... \n"+ //此处的\n还起到约束注释的作用
"( \n"+
" \\w[-.\\w]* #username \n"+
" @ \n"+
" [-\\w]+(\\.[-\\w]+)*\\.(com|edu|info) #hostname \n"+
") \n"+
"\\b \n",
Pattern.CASE_INSENSITIVE|Pattern.COMMENTS //大小写敏感,忽略注释
);
System.out.println(r.pattern()); //输出正则表达式
String text = "leejianhao1990@gmail.com";
Matcher matcher = r.matcher(text);
text = matcher.replaceAll("<a href=\"maito:$1\">$1</a>");
System.out.println(text);
}
}