打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
过滤html标签(java)
过滤html标签(java) 收藏
public  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;//返回文本字符串   
    }     

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/long0428/archive/2008/03/28/2225381.aspx
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
小区绣球花(4)
性骚案办成了谋反案
天呐!到40岁才知道,第一次看到这么全的文化常识,看完涨知识了,
Microsoft Office 2016精简、绿色版,自动永久激活
关于小学英语,胜算最大的学习布局!
漂亮!90个日常高频用字的行楷写法合集,既规整又潇洒! | 硬笔书法练字帖
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服