java关键字加链接
java关键字加链接 正则 代码如下: /* * To change this template, choose Tools | Templates * and open the template in the editor. */package javaapplication1;import java.util.HashMap;import java.util.regex.Pattern;/** * * @author dragon */public class Main { /** * @param args the command line arguments */ public static void main(String[] args) { String source ="从前有个山,山里有个庙,庙里有个老和尚。老和尚在给小和尚讲这样一个故事:从前有个山,山里有个庙....."; String keyword ="和尚"; String link ="http://www.yourlink.com"; System.out.println(Main.addKeywordLink(source,keyword,link)); } public static String addKeywordLink(String source,String keyword,String link){ return source.replaceAll("(?!((<.*?)|(<a.*?)))("+keyword+")(?!(([^<>]*?)>)|([^>]*?</a>))", "<a href=/""+link+"/">"+keyword+"</a>"); }}