zoukankan      html  css  js  c++  java
  • JAVA通过正则匹配html里面body标签的内容,去掉body标签

     /**
         *  获取html中body的内容 包含body标签
         * @param htmlStr  html代码
         * @return
         */
        public static String getBody(String htmlStr){
    
    
            String pattern = "<body[^>]*>([\s\S]*)<\/body>";
    
            Pattern p_body = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
            Matcher m_body = p_body.matcher(htmlStr);
            if (m_body.find()){
                return m_body.group();
            }
            return htmlStr;
        }
    
    
        /**
         * 取到html中body里面的内容 不包含body标签
         * @param htmlStr
         * @return
         */
        public static String removeBody(String htmlStr){
    
            /**
             * 获取html代码中body标签里的内容
             */
            htmlStr=getBody(htmlStr);
    
            //body开头标签
            String bodyEx_start = "<body[^>]*>";
    
            //body结尾标签
            String bodyEx_end = "<\/body>";
    
            Pattern p_script = Pattern.compile(bodyEx_start, Pattern.CASE_INSENSITIVE);
            Matcher m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); // 过滤script标签
    
            Pattern p_style = Pattern.compile(bodyEx_end, Pattern.CASE_INSENSITIVE);
            Matcher m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); // 过滤style标签
    
    
    
            return htmlStr;
        }

    如果要取得html代码中body里面的内容 不包含body标签

    直接调用 removeBody

    
    
    
    -----------------------有任何问题可以在评论区评论,也可以私信我,我看到的话会进行回复,欢迎大家指教------------------------ (蓝奏云官网有些地址失效了,需要把请求地址lanzous改成lanzoux才可以)
  • 相关阅读:
    深入理解并发编程 -- 多线程(一)
    使用Mybatis实现动态SQL(二)
    Java设计模式
    使用Mybatis实现动态SQL(一)
    Java
    Java安全(权限)框架
    List-LinkedList、set集合基础增强底层源码分析
    hadoop3.1.0 window win7 基础环境搭建
    springmvc传递有特殊字符的路径参数
    jhipster(springboot+datatable+jpa)后台分页,总结
  • 原文地址:https://www.cnblogs.com/pxblog/p/14716297.html
Copyright © 2011-2022 走看看