zoukankan      html  css  js  c++  java
  • 处理html内容,获取纯文本

    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    /**
     * @Author: lzw
     * @Description: 过滤html标签,转化为纯文本
     * @Date: Created in 13:56 2018-03-20
     */
    public class DelHtmlTagUtil {
    
        //将html转换为纯文本,此方法最后保留了&nbps空格,使用时注意将空格替换掉
        public static String delHTMLTag(String htmlStr) {
            String regEx_script = "<script[^>]*?>[\s\S]*?<\/script>"; //定义script的正则表达式
            String regEx_style = "<style[^>]*?>[\s\S]*?<\/style>"; //定义style的正则表达式
            String regEx_html = "<[^>]+>"; //定义HTML标签的正则表达式
    
            Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            Matcher m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); //过滤script标签
    
            Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            Matcher m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); //过滤style标签
    
            Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            Matcher m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); //过滤html标签
    
            return htmlStr.trim(); //返回文本字符串
        }
    
    }
    

      

  • 相关阅读:
    IEEE 网址
    知乎上非常棒的机器学习专栏
    怎样认识比你优秀的人并和他们成为朋友?
    影藏铜皮,显示原点与更改
    PCB检查事项,生成钻孔表
    布局-同样模块复用
    制作DIP Package及DIP焊盘制作,不规则焊盘制作
    制作SMD Package及SMD焊盘制作
    导入网络表
    导入DXF文件
  • 原文地址:https://www.cnblogs.com/wiseroll/p/9360184.html
Copyright © 2011-2022 走看看