1. 程式人生 > >java正則表示式去除html中所有的標籤和特殊HTML字元

java正則表示式去除html中所有的標籤和特殊HTML字元

關於java正則表示式去除html中所有的標籤和特殊HTML字元,結合我所做的專案總結的經驗:

總共分為三種:第一種適用於適用短的文章,將文章用正則表示式的方式拼接到程式碼中,有些繁瑣,其實不太實用。第二種就是直接將文件引入,進行更改,但是有一個小缺點,就是文件中的格式可能是utf-8格式的,需要更改成gbk格式的。第三種則是在程式碼中自動更改格式。由於是初學者,有很多不足,歡迎大家來補充。下面我分別給大家分享一下吧。

第一種:用於短資料,繁瑣一些。

package day0703;  
  
import java.util.regex.Matcher;  
import java.util.regex.Pattern;  
  
public class HtmlUtil { 
    private static final String regEx_script = "<script[^>]*?>[\\s\\S]*?<\\script>"; // 定義script的正則表示式  
    private static final String regEx_style = "<style [^>]*?>[\\s\\S]*?<\\/style >"; // 定義style的正則表示式  
    private static final String regEx_html = "<[^>]+>"; // 定義HTML標籤的正則表示式  
    private static final String regEx_space = "\\s*|\t|\r|\n";//定義空格回車換行符
    private static final String regEx_special = "\\&[a-zA-Z]{1,10};";//定義特殊字元的正則表示式,如 
      
    /** 
     * @param htmlStr 
     * @return 
     *  刪除Html標籤 
     *  用於短的資料,直接將文件複製貼上,寫入,但是每一個引號之前需要加“\”轉義符,用起來不方便
     */  
    public static String delHTMLTag(String htmlStr) {   
               
        Pattern p_script = Pattern.compile(regEx_script, Pattern.CASE_INSENSITIVE);  
        Matcher m_script = p_script.matcher(htmlStr);  
        htmlStr = m_script.replaceAll(""); // 過濾script標籤  
  
        Pattern p_style = Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);  
        Matcher m_style = p_style.matcher(htmlStr);  
        htmlStr = m_style.replaceAll(""); // 過濾style標籤  
  
        Pattern p_html = Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);  
        Matcher m_html = p_html.matcher(htmlStr);  
        htmlStr = m_html.replaceAll(""); // 過濾html標籤  
  
        Pattern p_space = Pattern.compile(regEx_space, Pattern.CASE_INSENSITIVE);  
        Matcher m_space = p_space.matcher(htmlStr);  
        htmlStr = m_space.replaceAll(""); // 過濾空格回車標籤 
        
        
        Pattern p_special = Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
        Matcher m_special = p_special.matcher(htmlStr);
        htmlStr = m_special.replaceAll(""); // 過濾特殊標籤
        return htmlStr.trim(); // 返回文字字串  
    }  
      
    public static String getTextFromHtml(String htmlStr){  
        htmlStr = delHTMLTag(htmlStr);  
        htmlStr = htmlStr.replaceAll(" ", "");    
        return htmlStr;  
    }  
      
    public static void main(String[] args) {  
        String str = "<P> </P>" +
        "<P class=MsoNormal style=\"LAYOUT-GRID-MODE: char; MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 150%; TEXT-INDENT: 24pt; mso-layout-grid-align: none\"><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\">2014</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">年</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\">12</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">月</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\">30</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">日,有訊息稱,某國家</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\">2015</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">年的工作重點將放在改革問題上,如兩個關鍵的清單(</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\">USML</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">)的修訂,以及上傳到雲伺服器的控制問題。</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\"><?xml:namespace prefix =\"o\" ns = \"urn:schemas-microsoft-com:office:office\" /><o:p></o:p></SPAN></P>" +
        "<P class=MsoNormal style=\"LAYOUT-GRID-MODE: char; MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 150%; TEXT-INDENT: 24pt; mso-layout-grid-align: none\"><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">責</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'; mso-ascii-theme-font: minor-fareast; mso-hansi-theme-font: minor-fareast\">·</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">沃爾夫指出,固定(</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\">EAR</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">)和小名(</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\">ITAR</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">)中關鍵術語的界定,該結果將於</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\">2015</SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">年初發布。同時,我也指出,目前,管理機構尚未解決加密標準的問題。這就涉及對“出口”一詞的界定,其中的一個元素就是“以某種方式”加密的控制資訊將不構成以雲端計算為目的的出口。而除了“出口”。例如,“基礎研究”是與管理出口規則緊密聯絡的。</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\"><o:p></o:p></SPAN></P>" +
        "<P class=MsoNormal style=\"LAYOUT-GRID-MODE: char; TEXT-ALIGN: right; MARGIN: 0cm 0cm 0pt; LINE-HEIGHT: 150%; TEXT-INDENT: 24pt; mso-layout-grid-align: none\" align=right><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">(李</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\"><SPAN style=\"mso-spacerun: yes\">  </SPAN></SPAN><SPAN style=\"FONT-SIZE: 12pt; FONT-FAMILY: 宋體; LINE-HEIGHT: 150%; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-fareast-font-family: 宋體; mso-fareast-theme-font: minor-fareast; mso-bidi-font-family: 'Times New Roman'\">明編譯)</SPAN><SPAN lang=EN-US style=\"FONT-SIZE: 12pt; FONT-FAMILY: 'Times New Roman','serif'; LINE-HEIGHT: 150%\"><o:p></o:p></SPAN></P>" +
        "<P> </P>";
        System.out.println(getTextFromHtml(str));  
    }  
}  


第二種:用於引入整個文件。(其中我的文件路徑為"D:/content.txt“,文件名稱為content.txt。

package day0703;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.regex.Pattern;


public class success {

/**
* 刪除Html標籤
* 將utf-8的文件手動改成gbk的,然後進行篩選輸出,用於多資料
* @param inputString
* @return
*/
public static String removeHtmlTag(String inputString) {
if (inputString == null)
return null;
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;
java.util.regex.Pattern p_special;
java.util.regex.Matcher m_special;
try {
// 定義script的正則表示式{或<script[^>]*?>[\\s\\S]*?<\\/script>
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
// 定義style的正則表示式{或<style[^>]*?>[\\s\\S]*?<\\/style>
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
// 定義HTML標籤的正則表示式
String regEx_html = "<[^>]+>";
// 定義一些特殊字元的正則表示式 如:     
String regEx_special = "\\&[a-zA-Z]{1,10};";


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_special = Pattern
.compile(regEx_special, Pattern.CASE_INSENSITIVE);
m_special = p_special.matcher(htmlStr);
htmlStr = m_special.replaceAll(""); // 過濾特殊標籤
textStr = htmlStr;
} catch (Exception e) {
e.printStackTrace();
}
return textStr;// 返回文字字串
}


/**
* 測試用的main函式
* @param args
*/
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
try {
FileReader fr = new FileReader("D:/content.txt");
BufferedReader br = new BufferedReader(fr);
String s = "";
while((s = br.readLine())!=null){
sb.append(s);
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String ssss = ReduceHtml2Text.removeHtmlTag(sb.toString());
System.out.println(ssss);
}
}


第三種:在程式碼中自動更改格式。直接

package day0703;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.regex.Pattern;


public class ReduceHtml2Text {


/**
* 刪除Html標籤
* 將utf-8的檔案直接轉成gbk輸出,用於多資料
* @param inputString
* @return
*/
public static String removeHtmlTag(String inputString) {
if (inputString == null)
return null;
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;
java.util.regex.Pattern p_special;
java.util.regex.Matcher m_special;
try {
// 定義script的正則表示式{或<script[^>]*?>[\\s\\S]*?<\\/script>
String regEx_script = "<[\\s]*?script[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?script[\\s]*?>";
// 定義style的正則表示式{或<style[^>]*?>[\\s\\S]*?<\\/style>
String regEx_style = "<[\\s]*?style[^>]*?>[\\s\\S]*?<[\\s]*?\\/[\\s]*?style[\\s]*?>";
// 定義HTML標籤的正則表示式
String regEx_html = "<[^>]+>";
// 定義一些特殊字元的正則表示式 如:     
String regEx_special = "\\&[a-zA-Z]{1,10};";


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_special = Pattern
.compile(regEx_special, Pattern.CASE_INSENSITIVE);
m_special = p_special.matcher(htmlStr);
htmlStr = m_special.replaceAll(""); // 過濾特殊標籤
textStr = htmlStr;
} catch (Exception e) {
e.printStackTrace();
}
return textStr;// 返回文字字串
}


/**
* 測試用的main函式
* 
* @param args
*/
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();


try {
File fr = new File("D:/content.txt");


BufferedReader br = new BufferedReader(new InputStreamReader(
new FileInputStream(fr), "UTF-8"));


String s = "";
while ((s = br.readLine()) != null) {
sb.append(s);
String str2 = new String(s.getBytes("GBK"), "GBK");


}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String asc= ReduceHtml2Text.removeHtmlTag(sb.toString());
System.out.println(asc);
}
}