输入检索词:中国 00
检索出数据eg: "中国00加油red!"
我们要对关键词标红显示到页面.应该显示  中国00加油00red!通常的做法是,String str = "中国00加油00red!";
str = str.replaceAll("中国","<font color=\"#FF0000\">"+"中国"+"</font>") ;//这时候str="<font color=\"#FF0000\">中国</font>00加油00red!"str = str.replaceAll("00","<font color="#FF0000">"+"00"+"</font>") ;//这时候str="<font color="#FF<font color="#FF0000"></font><font color="#FF0000"></font>\">中国</font><font color="#FF0000">00</font>加油red!"但是这样显示的数据不正确,因为第一次替换的时候有HTML代码的字符,并且包含了"00",那么第二次替换的时候就会出错.这种问题改如何解决呢?

解决方案 »

  1.   

    应该显示  中国00加油red! 
    输入错误!
      

  2.   

    str = str.replaceAll("中国","<font color=\"#FF0000\">"+"中国"+"</font>") ;
    改成这样
    str = str.replaceAll("中国","<font color=\"red">"+"中国"+"</font>") ;
    就不会有重复的00了
      

  3.   

    那如果检索的是 中国 red呢? 你那样不行!
      

  4.   

    你看一下下面的输出,匹配的第一组就是标签外的,正则式我写比较粗糙,你可以在加工一下public static void replace() {
    String s = "<xxx><yyyy>asdfasdffasdf";
    String reg = "<.+><.+>(.+[^<>])";
    Pattern p = Pattern.compile(reg);
    Matcher m = p.matcher(s);

    while(m.find()) {
    for(int i=0;i<=m.groupCount();i++) {
    System.out.println(m.group(i));
    }
    }
    }
      

  5.   


    public String convert(String text, String patternString, HashMap map){
    PatternMatcher matcher;
    PatternCompiler compiler;
    Pattern pattern;
    PatternMatcherInput input;
    MatchResult result;

    StringBuffer textBuf = new StringBuffer(text);
    try{

    compiler = new Perl5Compiler();
    pattern = compiler.compile(patternString);
    matcher = new Perl5Matcher();
    input = new PatternMatcherInput(text.toString());

    int offset = 0;
    while (matcher.contains(input, pattern)) {
    result = matcher.getMatch();
    int len = result.length();
    String key = result.toString();
    String value = (String) map.get(key);
    textBuf.replace(result.beginOffset(0) + offset, result.endOffset(0)
    + offset, value);
    offset += value.length() - len;
    }
    }catch (Exception e) {
    e.printStackTrace();
    } return textBuf.toString();
    }


    public static String getContent(String content){

    SetImg si = new SetImg();
    String pattern = "\\{[a-zA-Z0-9]+\\}";
    HashMap map = new HashMap();
    for (int i = 1;i <= 24;i++){
    if (i < 10){
    map.put("{face20" + i +"}", "<img src='img/face20" + i + ".gif'/>");
    }
    if (i >= 10 && i <= 24){
    map.put("{face2" + i +"}", "<img src='img/face2" + i + ".gif'/>");
    }
    }
    return si.convert(content, pattern, map);
    }这是我前天写的
    这样是表 字符 替换成图片
    跟你说的差不多 你改改 就可以用了
    希望有帮助哦
    ^_*
      

  6.   

    假如:要检索的是1245
    关键字 2 >4
    第一次替换后:1<font color='red'>2</font>45
    那么第二次会把>4检索出来,然后替换.
    这样就不是我们的结果了.
      

  7.   


    public static void main(String[] args) {
    List<String> inputs=new ArrayList<String>();
    inputs.add("中国");
    inputs.add("00");
    inputs.add("附件");
    inputs.add("F");
    String str="sdflwef玩儿看中国F看了我味儿来开发看了我玩儿看了附件中国快乐我爱你00";
    StringBuffer resStr=new StringBuffer("");
    addChild(str,inputs,resStr);
    System.out.println(resStr.toString());
    }

    /**
     * 多关键字查询表红,避免后面的关键字成为特殊的HTML语言代码
     * @param str  检索结果
     * @param inputs 关键字集合
     * @param resStr 表红后的结果
     */
    public static void addChild(String str,List<String> inputs,StringBuffer resStr){
    int index=str.length();//用来做为标识,判断关键字的下标
    String next="";//保存str中最先找到的关键字
    for (int i = inputs.size() -1 ; i>= 0;i--) {
    String theNext=inputs.get(i);
    int theIndex=str.indexOf(theNext);
    if(theIndex==-1){//过滤掉无效关键字
    inputs.remove(i);
    }else if(theIndex<index){
    index=theIndex;//替换下标
    next=theNext;
    }
    } //如果条件成立,表示串中已经没有可以被替换的关键字,否则递归处理
    if(index==str.length()){
    resStr.append(str);
    }else{
    resStr.append(str.substring(0,index));
    resStr.append("<font color='#FF000'>"+str.substring(index,index+next.length())+"</font>");
    String str1=str.substring(index+next.length(),str.length());
    addChild(str1,inputs,resStr);//剩余的字符串继续替换
    }
    }这是一种解决办法.但是正则表达式应该也可以做到.还没有研究出来.