booby325 发表于 2013-2-7 15:21:54

java去除html tags

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; }  
页: [1]
查看完整版本: java去除html tags