1. 程式人生 > >java 使用正則表達式過濾HTML中標簽

java 使用正則表達式過濾HTML中標簽

clas 表達式 sage ase .get isempty compile return ins

 1 /**
 2      * 去掉文本中的html標簽
 3      *
 4      * @param inputString
 5      * @return
 6      */
 7     public static String html2Text(String inputString) {
 8         if (StringUtils.isEmpty(inputString)) {
 9             return null;
10         }
11         String htmlStr = inputString;
12         String textStr = "";
13 java.util.regex.Pattern p_script; 14 java.util.regex.Matcher m_script; 15 java.util.regex.Pattern p_style; 16 java.util.regex.Matcher m_style; 17 java.util.regex.Pattern p_html; 18 java.util.regex.Matcher m_html; 19 20 java.util.regex.Pattern p_html1;
21 java.util.regex.Matcher m_html1; 22 23 try { 24 String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>"; // 定義script的正則表達式{或<script[^>]*?>[\\s\\S]*?<\\/script> 25 // } 26 String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>"; //
定義style的正則表達式{或<style[^>]*?>[\\s\\S]*?<\\/style> 27 // } 28 String regEx_html = "<[^>]+>"; // 定義HTML標簽的正則表達式 29 String regEx_html1 = "<[^>]+"; 30 p_script = Pattern.compile(regEx_script, 31 Pattern.CASE_INSENSITIVE); 32 m_script = p_script.matcher(htmlStr); 33 htmlStr = m_script.replaceAll(""); // 過濾script標簽 34 35 p_style = Pattern 36 .compile(regEx_style, Pattern.CASE_INSENSITIVE); 37 m_style = p_style.matcher(htmlStr); 38 htmlStr = m_style.replaceAll(""); // 過濾style標簽 39 40 p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE); 41 m_html = p_html.matcher(htmlStr); 42 htmlStr = m_html.replaceAll(""); // 過濾html標簽 43 44 p_html1 = Pattern 45 .compile(regEx_html1, Pattern.CASE_INSENSITIVE); 46 m_html1 = p_html1.matcher(htmlStr); 47 htmlStr = m_html1.replaceAll(""); // 過濾html標簽 48 49 textStr = htmlStr; 50 51 // 替換&amp;nbsp; 52 textStr = textStr.replaceAll("&amp;", "").replaceAll("nbsp;", ""); 53 54 } catch (Exception e) { 55 System.err.println("Html2Text: " + e.getMessage()); 56 } 57 58 return textStr;// 返回文本字符串 59 }

  

  

/** * 去掉文本中的html標簽 * * @param inputString * @return */ public static String html2Text(String inputString) { if (StringUtils.isEmpty(inputString)) { return null; } String htmlStr = inputString; 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;
java.util.regex.Pattern p_html1; java.util.regex.Matcher m_html1;
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標簽的正則表達式 String regEx_html1 = "<[^>]+"; 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標簽
p_html1 = Pattern .compile(regEx_html1, Pattern.CASE_INSENSITIVE); m_html1 = p_html1.matcher(htmlStr); htmlStr = m_html1.replaceAll(""); // 過濾html標簽
textStr = htmlStr;
// 替換&amp;nbsp; textStr = textStr.replaceAll("&amp;", "").replaceAll("nbsp;", "");
} catch (Exception e) { System.err.println("Html2Text: " + e.getMessage()); }
return textStr;// 返回文本字符串 }

java 使用正則表達式過濾HTML中標簽