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>");
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值