六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 58|回复: 0

java去除html tags

[复制链接]

升级  39.33%

25

主题

25

主题

25

主题

秀才

Rank: 2

积分
109
 楼主| 发表于 2013-2-7 15:21:54 | 显示全部楼层 |阅读模式
public static String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\/script>";   public static String regEx_style = "<style[^>]*?>[\\s\\S]*?<\\/style>";  public static String regEx_html = "<[^>]+>";  public static Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE); public static Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE); public static Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  public static String getOptimizedData(String inputString) {      if (inputString == null) {           return inputString;      }        //stripping script tags whether the tag contains "\n" or "\r" or not.      Matcher m_script = p_script.matcher(inputString);      String htmlStr = m_script.replaceAll("");       //stripping style tags whether the tag contains "\n" or "\r" or not.      Matcher m_style = p_style.matcher(htmlStr);      htmlStr = m_style.replaceAll("");       //stripping html tags but continue to have the "\n" and "\r" in right place.      Matcher m_html = p_html.matcher(htmlStr);      htmlStr = m_html.replaceAll("");       return htmlStr; }  
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表