java正则表达式去除html中所有的标签和特殊HTML字符

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_35835118/article/details/75647559

关于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);
}
}



猜你喜欢

转载自blog.csdn.net/qq_35835118/article/details/75647559