论坛首页 Java企业应用论坛

请教appendReplacement的用法

浏览 6235 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-07-04  
我想处理一段文字,替换其中的关键字为加红,现在写了下面的程序,有两个方法,都是用来处理加红效果的,然而,都没有达到预期效果,其结果如下:
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestGetNewStr {

	public static String getNewStr(String str,String regEx,StringBuffer pre) {
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		int startIndex = 0;
		int endIndex = 0;
		String oldStr = "";
		String newStr = "";
		boolean b = true;
		if (m.find()) {
			oldStr = m.group();
			newStr = "<span style=\"color:red\">"+ oldStr +"</span>";
			startIndex = m.start();
			endIndex = m.end();	
			pre.append(str.substring(0, startIndex) + newStr);
			String temp = str.substring(endIndex);
			getNewStr(temp,regEx,pre);
			b = false;
		}
		if (b) {
			pre.append(str);
		}
		return pre.toString();
	}

	public static String getNewStr2(String str,String regEx,StringBuffer pre) {
    	Pattern p = Pattern.compile(regEx);
    	Matcher m = p.matcher(str);
    	while (m.find()) {
    		m.appendReplacement(pre, "<span style=\"color:red\">$2</span>");
    	}
    	m.appendTail(pre);
		return pre.toString();
	}

	public static void main(String[] args) {
		String str = "nbasfksdNbafspandjdk<a>dksfdkspanBajfkdadd</a><span style=\"color:red\">djksnbakdspanjksjf</span>djfkspandjdfknba";
		String regEx = "(^|[^</])([Ss][Pp][Aa][Nn])($|[^>])";
		StringBuffer pre = new StringBuffer();
		System.out.println(getNewStr(str,regEx,pre));
		StringBuffer pre2 = new StringBuffer();
		System.out.println(getNewStr2(str,regEx,pre2));		
	}
}


运行结果 :

nbasfksdNba<span style="color:red">fspand</span>jdk<a>dksfd<span style="color:red">kspanB</span>ajfkdadd</a><span style="color:red">djksnbak<span style="color:red">dspanj</span>ksjf</span>djf<span style="color:red">kspand</span>jdfknba


nbasfksdNba<span style="color:red">span</span>jdk<a>dksfd<span style="color:red">span</span>ajfkdadd</a><span style="color:red">djksnbak<span style="color:red">span</span>ksjf</span>djf<span style="color:red">span</span>jdfknba


请问我如何做呢?因为要匹配大小写,所一我的正贼表达式写成那样了,比如,如果你搜索nba的匹配的话,那就是(^|[^</])([Nn][Bb][Aa])($|[^>])了。


十分感谢您的关注。。。
   发表时间:2008-07-07  
问题解决了,正确代码如下:

package com.search.test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class TestGetNewStr {
	public static String getNewStr(String str,String regEx,StringBuffer pre) {
		Pattern p = Pattern.compile(regEx);
		Matcher m = p.matcher(str);
		int startIndex = 0;
		int endIndex = 0;
		String oldStr = "";
		String newStr = "";
		boolean b = true;
		if (m.find()) {
			oldStr = m.group(2);
			newStr = "<span style=\"color:red\">"+ oldStr +"</span>";
			startIndex = m.start(2);
			endIndex = m.end(2);	
			pre.append(str.substring(0, startIndex) + newStr);
			String temp = str.substring(endIndex);
			getNewStr(temp,regEx,pre);
			b = false;
		}
		if (b) {
			pre.append(str);
		}
		return pre.toString();
	}

	public static String getNewStr2(String str,String regEx,StringBuffer pre) {
    	Pattern p = Pattern.compile(regEx);
    	Matcher m = p.matcher(str);
    	while (m.find()) {
    		m.appendReplacement(pre, "$1<span style=\"color:red\">$2</span>$3");
    	}
    	m.appendTail(pre);
		return pre.toString();
	}

	public static void main(String[] args) {
		String str = "nbasfksdNbafspandjdk<a>dksfdkspanBajfkdadd</a><span style=\"color:red\">djksnbakdspanjksjf</span>djfkspandjdfknba";
		//String regEx = "(^|[^</])([Ss][Pp][Aa][Nn])($|[^>])";
		String regEx = "(^|[^</])([Nn][Bb][Aa])($|[^>])";
		StringBuffer pre = new StringBuffer();
		System.out.println(getNewStr(str,regEx,pre));
		StringBuffer pre2 = new StringBuffer();
		System.out.println(getNewStr2(str,regEx,pre2));	
	}
}

0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics