excel或word保存htm,读io把关键字高亮

 private String handleKeyword(String fileContent, String keyword){
        int pos = 0;// 记录已读取到文件的位置
        String replacement = "<B style='color:black;background-color:#FF00FF'>"+keyword+"</B>";
        StringBuffer sb = new StringBuffer();
        
        String regex = "<td[\\W\\w]*?</td>";// 单元格数据
        Pattern pt = Pattern.compile(regex);
        Matcher mt = pt.matcher(fileContent);
        while (mt.find()){
            String match = mt.group();
            int index = fileContent.indexOf(match, pos);
            String str = fileContent.substring(pos, index);
            sb.append(str);// 不匹配的部分不用处理
            pos = index;
            
            // 对匹配的部分,即单元格数据进行处理
            int index1 = match.indexOf(">");
            sb.append(match.substring(0, index1 + 1));
            String value = match.substring(index1 + 1, match.length() - 5);
            if(value.indexOf(keyword) > -1){// 含关键字,需要处理
                // 对单元格内的文本,要求只能有一种样式才可以调用handleText方法。
                // 否则,关键词与样式正好相同时还是会造成误判,参考handleKeyword2方法。
                sb.append(HtmlUtils.handleText(value, keyword, replacement));
            }else {// 不含关键字,不必处理
                sb.append(value);    
            }
            sb.append("</td>");
            pos = pos + match.length();
        } 
        sb.append(fileContent.substring(pos));// 加上末尾不匹配的部分
        //System.out.println(sb.toString());
        return sb.toString();

    }



 private String handleKeyword2(String fileContent, String keyword){
        int pos = 0;// 记录已读取到文件的位置
        String replacement = "<B style='color:black;background-color:#FF00FF'>"+keyword+"</B>";
        StringBuffer sb = new StringBuffer();
        
        String regex = "<span[\\W\\w]*?</span>";// span数据
        Pattern pt = Pattern.compile(regex);
        Matcher mt = pt.matcher(fileContent);
        while (mt.find()){
            String match = mt.group();
            int index = fileContent.indexOf(match, pos);
            String str = fileContent.substring(pos, index);
            sb.append(str);// 匹配的部分不用处理
            pos = index;
            
            // 对匹配的部分,即SPAN的数据进行处理
            int index1 = match.indexOf(">");
            sb.append(match.substring(0, index1 + 1));
            String value = match.substring(index1 + 1, match.length() - 7);
            if(value.indexOf(keyword) > -1){// 含关键字,需要处理
                sb.append(HtmlUtils.handleHtm(value, keyword, replacement));
            }else {// 不含关键字,不必处理
                sb.append(value);
            }
            sb.append("</span>");
            pos = pos + match.length();
        } 
        
        sb.append(fileContent.substring(pos));// 加上末尾不匹配的部分
        //System.out.println(sb.toString());
        return sb.toString();
    }


 public static String handleText(String text, String keyword, String replacement){
        int type = getType(keyword);
        String regex;
        if(type == 0 || replacement == null){// 关键字为空或者替换字符串为null,直接返回
            return text;
        } else if(type == 1){// 关键字是数字
            regex = "(-?\\d+)(\\.\\d+)|-?\\d+";
        } else if(type == 2){// 关键字是字母
            regex = "[a-zA-Z]+";
        } else {// 关键字是汉字或者其它
            return text.replaceAll(keyword, replacement);
        }
        int pos = 0;
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(text);// 匹配所有的英文单词
        while(m.find()){
            String match = m.group();
            int index = text.indexOf(match, pos);
            String str = text.substring(pos, index);
            sb.append(str);
            pos = index;
            
            if(keyword.equals(match)){//关键字和匹配到的单词相同则替换
                sb.append(replacement);
            } else {
                sb.append(match);
            }
            pos = pos + match.length();
        }
        sb.append(text.substring(pos));
        return sb.toString();
    }



public static String handleHtm(String html, String keyword, String replacement){
        int pos = 0;
        StringBuffer sb = new StringBuffer();
        Pattern p = Pattern.compile(REGEX_HTM);//匹配HTM标记
        Matcher m = p.matcher(html);
        while(m.find()){
            String match = m.group();
            int index = html.indexOf(match, pos);
            
            // 取不匹配的部分,即页面上直接显示的内容进行后续处理
            String str = html.substring(pos, index);
            if(str.indexOf(keyword) > -1){// 单元格数据中含关键字,需要处理
                sb.append(handleText(str, keyword, replacement));
            } else {// 单元格数据中不含关键字,不必处理
                sb.append(str);    
            }
            pos = pos + str.length();
            
            // 匹配的部分不用处理
            sb.append(match);
            pos = pos + match.length();
        }
        
        // 末尾不匹配的部分也需要处理
        String end = html.substring(pos);
        sb.append(handleText(end, keyword, replacement));
        return sb.toString();
    }


public static int getType(String str){
        if(StringUtils.isBlank(str)){
            return 0;
        }
        // 匹配数字
        Pattern p = Pattern.compile("(-?\\d+)(\\.\\d+)|-?\\d+");
        Matcher m = p.matcher(str);
        if(m.matches()){
            return 1;
        }
        // 匹配26个英文字母
        p = Pattern.compile("[a-zA-Z]+");
        m = p.matcher(str);
        if(m.matches()){
            return 2;
        }
        // 匹配汉字
        p = Pattern.compile("[\u4e00-\u9fa5]+");
        m = p.matcher(str);
        if(m.matches()){
            return 3;
        }
        
        return 4;
    }

/**
     * 正则表达式-HTM标记
     */
    public static final String REGEX_HTM = "</[\\W\\w]*?>|<[\\W\\w]*?/>|<[\\W\\w]*?>";



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值