JAVA通过正则匹配html里面body标签的内容

JAVA通过正则匹配html里面body标签的内容,具体代码如下,如果要取得html代码中body里面的内容 不包含body标签,直接调用removeBody /** * 获取html中body的内容 包含body标签 * @param htmlStr html代码 * @return */ public static String getBody(String

JAVA通过正则匹配html里面body标签的内容,具体代码如下,如果要取得html代码中body里面的内容 不包含body标签,直接调用 removeBody

/**
     *  获取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;
    }
 
本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

相关文档推荐

php数组通过array_push函数进行排序及选择排序,具体实例代码如下: ?php //通过array_push函数进行排序$arr = array(8,1,3,4,6,9,5,2,7);array_push($arr, 4);sort($arr);print_r($arr);//选择排序$arr=array(8,1,3,4,6,9,5,2,7);$length =count($arr);//9fo
layui.open 通过get和post方式提交数据的两种方式,示例代码如下,希望可以帮到您。 1.layui.open原生是通过get提交数据的: var url = "/train/class/stage?" + jQuery.param(stage); window.layerIndex = layer.open({ type: 2, title: "编辑阶段", shadeCl
我们要验证一段信息是否为ip地址段,用php代码怎么写呢?具体实例代码如下: /** * 匹配IP地址 * @param string $subject * @return bool */function checkVailIp(string $subject){ $pattern = "/(1\d{2}|2[0-4]\d|25[0-5]|[1-9]\d|[1-9])\."; $pattern .="(
在javascript中将GBK转UTF-8的实例代码,具体代码如下,希望能够帮到您: script type="text/javascript" var easyUTF8 = function(gbk){ if(!gbk){return '';} var utf8 = []; for(var i=0;igbk.length;i++){ var s_str = gbk.charAt(i); if(!(/^%u/i.test(e
How can I use a C++ library from node.js?(如何使用 node.js 中的 C++ 库?)
本文给大家介绍Javascript js中实现和PHP一样的时间戳格式化函数的方法,具有一定的参考借鉴价值,需要的朋友可以参考下,我们知道在php中有一个date()函数,可以方便的把时间戳格式化为时间字符串。可是在js中,我们要想实现这种效果,要写好多好多代码,非