Android Html 过滤标签

来源:互联网 发布:php对小数的处理 编辑:程序博客网 时间:2024/05/18 00:27

        String content="<div class=\\\"para\\\" label-module=\\\"para\\\" style=\\\"word-wrap: break-word; margin-bottom: 5px; zoom: 1;\\\">巴斯大学(University of Bath)<span style=\\\"position: relative; vertical-align: baseline; top: -0.5em; margin-left: 2px; cursor: default; padding-right: 2px; padding-left: 2px;\\\">[1]</span><a class=\\\"sup-anchor\\\" name=\\\"ref_[1]_16367\\\" style=\\\"position: relative; top: -50px;\\\">&nbsp;</a>&nbsp;是一所以科研为向导的英国顶尖名校,科研实力被评定为世界领先<span style=\\\"position: relative; vertical-align: baseline; top: -0.5em; margin-left: 2px; cursor: default; padding-right: 2px; padding-left: 2px;\\\">[2]</span><a class=\\\"sup-anchor\\\" name=\\\"ref_[2]_16367\\\" style=\\\"position: relative; top: -50px;\\\">&nbsp;</a>&nbsp;。</div><div class=\\\"para\\\" label-module=\\\"para\\\" style=\\\"word-wrap: break-word; margin-bottom: 5px; zoom: 1;\\\">巴斯大学是全英前10的著名学府,历年最高排名全英第4(分别于2003年和2015年)<span style=\\\"position: relative; vertical-align: baseline; top: -0.5em; margin-left: 2px; cursor: default; padding-right: 2px; padding-left: 2px;\\\">[3]</span><a class=\\\"sup-anchor\\\" name=\\\"ref_[3]_16367\\\" style=\\\"position: relative; top: -50px;\\\">&nbsp;</a>&nbsp;。成立于1966年,现任校监为女王<a target=\\\"_blank\\\" href=\\\"http://baike.baidu.com/view/47650.htm\\\">伊丽莎白二世</a>的三子 -&nbsp;<a target=\\\"_blank\\\" href=\\\"http://baike.baidu.com/subview/3035700/11062139.htm\\\" data-lemmaid=\\\"8488157\\\">爱德华王子</a>, 威塞克斯伯爵<span style=\\\"position: relative; vertical-align: baseline; top: -0.5em; margin-left: 2px; cursor: default; padding-right: 2px; padding-left: 2px;\\\">[4]</span><a class=\\\"sup-anchor\\\" name=\\\"ref_[4]_16367\\\" style=\\\"position: relative; top: -50px;\\\">&nbsp;</a>&nbsp;。</div><div class=\\\"para\\\" label-module=\\\"para\\\" style=\\\"word-wrap: break-word; margin-bottom: 5px; zoom: 1;\\\"><img src=\\\"http://g5bk.com:8080/MojieProject/file/smallContentFile/418ef337-8b48-4a2d-bf59-91f0a33259c3_47802525_5(1).jpg\\\" w=\\\"2599\\\" h=\\\"1640\\\" style=\\\"width: 778px;\\\"/><br></div><div class=\\\"para\\\" label-module=\\\"para\\\" style=\\\"word-wrap: break-word; margin-bottom: 5px; zoom: 1;\\\">大学位于<a target=\\\"_blank\\\" href=\\\"http://baike.baidu.com/view/58688.htm\\\">英格兰</a>南部的世界遗产城市-<a target=\\\"_blank\\\" href=\\\"http://baike.baidu.com/subview/89823/12443073.htm\\\" data-lemmaid=\\\"1620585\\\">巴斯</a>。根据英国各媒体近十年公布的英国大学排行榜,巴斯大学被稳固的确立为<span style=\\\"position: relative; vertical-align: baseline; top: -0.5em; margin-left: 2px; cursor: default; padding-right: 2px; padding-left: 2px;\\\">[3]</span><a class=\\\"sup-anchor\\\" name=\\\"ref_[3]_16367\\\" style=\\\"position: relative; top: -50px;\\\">&nbsp;</a>&nbsp;英国排行前十的大学。</div><div class=\\\"para\\\" label-module=\\\"para\\\" style=\\\"word-wrap: break-word; margin-bottom: 5px; zoom: 1;\\\"><a target=\\\"_blank\\\" href=\\\"http://baike.baidu.com/view/2416950.htm\\\">巴斯大学管理学院</a>被公认为英国最好的商学院之一,&nbsp;在业界有极高的声誉,每年有大量本科毕业生进入伦敦顶级投行工作,在2016年完全大学指南排名中位居第1位。</div><div class=\\\"para\\\" label-module=\\\"para\\\" style=\\\"word-wrap: break-word; margin-bottom: 5px; zoom: 1;\\\"><img src=\\\"http://g5bk.com:8080/MojieProject/file/smallContentFile/418ef337-8b48-4a2d-bf59-91f0a33259c3_slide1-large(1).jpg\\\" w=\\\"640\\\" h=\\\"480\\\" style=\\\"width: 640px;\\\"/><br></div><div class=\\\"para\\\" label-module=\\\"para\\\" style=\\\"word-wrap: break-word; margin-bottom: 5px; zoom: 1;\\\">巴斯大学在2015完全大学指南英国大学排名上��居第8位<span style=\\\"position: relat\n";


    /**
     * 定义script的正则表达式
     */
    private static final String REGEX_SCRIPT = "<script[^>]*?>[\\s\\S]*?<\\/script>";
    /**
     * 定义style的正则表达式
     */
    private static final String REGEX_STYLE = "<style[^>]*?>[\\s\\S]*?<\\/style>";
    /**
     * 定义HTML标签的正则表达式
     */
    private static final String REGEX_HTML = "<[^>]+>";
    /**
     * 定义空格回车换行符
     */
    private static final String REGEX_SPACE = "\\s*|\t|\r|\n";



    public static String delHTMLTag(String htmlStr) {
        // 过滤script标签
        Pattern p_script = Pattern.compile(REGEX_SCRIPT, Pattern.CASE_INSENSITIVE);
        Matcher m_script = p_script.matcher(htmlStr);
        htmlStr = m_script.replaceAll("");
        // 过滤style标签
        Pattern p_style = Pattern.compile(REGEX_STYLE, Pattern.CASE_INSENSITIVE);
        Matcher m_style = p_style.matcher(htmlStr);
        htmlStr = m_style.replaceAll("");
        // 过滤html标签
        Pattern p_html = Pattern.compile(REGEX_HTML, Pattern.CASE_INSENSITIVE);
        Matcher m_html = p_html.matcher(htmlStr);
        htmlStr = m_html.replaceAll("");
        // 过滤空格回车标签
        Pattern p_space = Pattern.compile(REGEX_SPACE, Pattern.CASE_INSENSITIVE);
        Matcher m_space = p_space.matcher(htmlStr);
        htmlStr = m_space.replaceAll("");




        Pattern a_space = Pattern.compile("&nbsp;", Pattern.CASE_INSENSITIVE);
        Matcher b_space = a_space.matcher(htmlStr);
        htmlStr = b_space.replaceAll("");


        return htmlStr.trim(); // 返回文本字符串
    }


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 微信账号忘记密码上面有钱怎么办 蘑菇街账号忘记了里面有钱怎么办 手机注册的百度云帐号换号怎么办 网易云音乐sd卡写入失败怎么办? 玩客云涌u盘满了怎么办 手机安装杜比音效失败死机该怎么办 网易云音乐下载额度用完了怎么办 苹果手机付费app连续扣费怎么办 oppoO粉卡的流量用没了怎么办 qq邮箱里的文件过期了怎么办 163邮箱里的文件过期了怎么办 做人事的给员工漏交社保了怎么办 公司漏交了员工一个月的社保怎么办 小米4c能充电不能开机怎么办 小米手机关机开不开机该怎么办 小米5s关机开不开机怎么办 老板跑路了员工社保还挂着怎么办 红米5连wifi很慢怎么办 电脑百度网盘下载速度慢怎么办 新手机与旧手机同步了怎么办 百度网盘2t满了怎么办 魅族手机相册的密码忘了怎么办 手机百度网盘本地空间不足怎么办 百度网盘保存视频内存不够怎么办 别人的百度网盘链接打不开怎么办 百度网盘视频插件升级失败怎么办 退苹果id重新登陆照片没了怎么办 苹果升级后照片效果没了怎么办 360云盘个人云盘停止服务怎么办 小米手机账号掉了手机卡没了怎么办 百度网盘下载后不能注册怎么办 微信发出的文件无法撤回怎么办 微信群发错消息无法撤回怎么办 百度网盘登录要验证码怎么办 百度网盘备份记录怎么办能删掉 手机酷狗听歌耳机声音太小了怎么办 苹果6s微信通话声音变粗怎么办 千千静听多个列表合成一个了怎么办 别人用手机号注册了邮箱怎么办 微信身份信息验证未通过怎么办 快递地址填错了已经发到了怎么办