浏览 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])($|[^>])了。 十分感谢您的关注。。。 声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |
发表时间: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)); } } |
|
返回顶楼 | |