import java.util.regex.Pattern;
 
/**
 * 过滤标签字符串,返回纯文本
 *
 */
public class ChangePlainText {
 
    public static void main(String[] args) {
 
        String test="<b>hi</b></br><h1>hello~</h1><哈哈>";
 
        String b=ChangePlainText.Html2Text(test);
 
        System.out.println(b);
    }
 
    public static String Html2Text(String inputString) {
        String htmlStr = inputString; // 含html标签的字符串
        String textStr = "";
        java.util.regex.Pattern p_script;
        java.util.regex.Matcher m_script;
        java.util.regex.Pattern p_style;
        java.util.regex.Matcher m_style;
        java.util.regex.Pattern p_html;
        java.util.regex.Matcher m_html;
 
        try {
            String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定义script的正则表达式{或<script[^>]*?>[\\s\\S]*?<\\/script>
                                                                                                        // }
            String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; // 定义style的正则表达式{或<style[^>]*?>[\\s\\S]*?<\\/style>
                                                                                                    // }
            String regEx_html = "<[^>]+>"; // 定义HTML标签的正则表达式
 
            p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);
            m_script = p_script.matcher(htmlStr);
            htmlStr = m_script.replaceAll(""); // 过滤script标签
 
            p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
            m_style = p_style.matcher(htmlStr);
            htmlStr = m_style.replaceAll(""); // 过滤style标签
 
            p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
            m_html = p_html.matcher(htmlStr);
            htmlStr = m_html.replaceAll(""); // 过滤html标签
 
            textStr = htmlStr;
 
        } catch (Exception e) {
            System.err.println("Html2Text: " + e.getMessage());
        }
 
        return textStr;// 返回文本字符串
    }
 

}

Java过滤任意(script,html,style)标签符,返回纯文本--封装类的更多相关文章

  1. JS动态引入js,CSS——动态创建script/link/style标签

    一.动态创建link方式 我们可以使用link的方式.如下代码所示. 二.动态创建style方式 但是,这样的话,需要加载整个css文件,但是那样有可能浪费一个http请求并占用一个服务器请求数,并等 ...

  2. JS动态引入js、CSS动态创建script/link/style标签

    一.动态创建link方式 我们可以使用link的方式.如下代码所示. function addCssByLink(url){ var doc=document; var link=doc.create ...

  3. .Net(C#)用正则表达式清除HTML标签(包括script和style),保留纯本文(UEdit中编写的内容上传到数据库)

    去官网下载,本Demo用的MVC模式 下载地址:http://ueditor.baidu.com/website/download.html 加入文件夹中的结构: 引入了函数公式的图标: @{ Vie ...

  4. Java 去除HTML标签转化成纯文本

    package com.ahgw.common.global; import java.util.regex.Pattern; /** * 截取HTML代码 * * @author YangJunpi ...

  5. JS 过滤HTML标签,取得纯文本

    一.过滤掉所有HTML标签如下: str.innerHTML.replace(/<.*?>/g,"") 二.过滤掉带属性的某一个标签,如<span class=' ...

  6. 过滤eWebeditor等富文本中html标签,获得纯文本信息

    /// <summary> /// 过滤html标签 /// </summary> /// <param name="Htmlstring">& ...

  7. java文章显示内容部分(将html转成纯文本)

    public static String splitAndFilterString(String input, int length) { if (input == null || input.tri ...

  8. iOS中使用正则表达式去掉HTML中的标签元素获得纯文本的方法

    content是根据网址获得的网页源码字符串 - (NSString *)changeToString:(NSString *)content { NSRegularExpression *regul ...

  9. Java过滤掉字符串中的html标签、style标签、script标签

    使用正则表达式 import java.util.regex.Matcher; import java.util.regex.Pattern; public class HTMLSpirit{ pub ...

随机推荐

  1. AttributeError: 'str' object has no attribute 'decode'

    ue = e.decode('latin-1')修改为: ue = e.encode('ascii', 'strict')

  2. 多播知识by 陈胜君

    简单的讲一下多拨的说明:一.多拨分物理多拨和虚拟多拨. 物理多拨是电信老套餐,就是一个宽带支持四个内网设备同时拨号上网,即2004年以前,允许家里四台电脑直接连LAN网口启动拨号,同时允许四拨在线.现 ...

  3. 使用非root用户启动tomcat

    以下操作均为以root用户运行1.添加tomcat用户组 /usr/sbin/groupadd tomcat 2.添加tomcat用户,并限制登录 /usr/sbin/useradd -s /bin/ ...

  4. ioctl socket getsockopt

    一 ioctl 函数产生原因: 虽然在文件操作结构体"struct file_operations"中有很多对应的设备操作函数,但是有些命令是实在找不到对应的操作函数.如CD-RO ...

  5. html 简单学习

    通过记事本,依照以下四步来创建您的第一张网页. 步骤一:启动记事本 如何启动记事本: 开始    所有程序        附件            记事本 步骤二:用记事本来编辑 HTML 在记事本 ...

  6. 中文chrome font-size 10px,11px,12px,rem只为12px解决办法

    问题来源: html { font-size: 10px; -webkit-tap-highlight-color: rgba(0, 0, 0, 0); } .form-signin { max-wi ...

  7. Redis 常见面试题

    使用Redis有哪些好处? 速度快 基于内存,避免了磁盘I/O的瓶颈. 单进程单线程,减少了线程上下文切换的开销 利用队列技术将并行访问变为串行访问,消除了传统数据库并发访问控制锁的开销. Redis ...

  8. LeetCode301. Remove Invalid Parentheses

    Remove the minimum number of invalid parentheses in order to make the input string valid. Return all ...

  9. C#窗口矩形区域着色

    C#写的一个GUI窗口,有几百个矩形区域.每个矩形区域的颜色随时都可能改变,并且多次改变. 我放弃使用label绘制矩形,因为效果不好.拖控件的界面使用power packs中的rectanglesh ...

  10. PHP 文件夹操作「复制、删除、查看大小、重命名」递归实现

    PHP虽然提供了 filesize.copy.unlink 等文件操作的函数,但是没有提供 dirsize.copydir.rmdirs 等文件夹操作的函数(rmdir也只能删除空目录).所以只能手动 ...