java正则匹配非html字符串_java正则表达式去除html中所有的标签和特殊HTML字符(以开头的)...
packagecom.comcons.utils;importjava.io.BufferedReader;importjava.io.FileNotFoundException;importjava.io.FileReader;importjava.io.IOException;importjava.util.regex.Pattern;public classReduceHtml2Text {/*** 刪除Html標(biāo)簽
*@paraminputString
*@return
*/
public staticString removeHtmlTag(String inputString) {if (inputString == null)return null;
String htmlStr= inputString; //含html標(biāo)簽的字符串
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的正則表達(dá)式{或
String regEx_script = "<[\s]*?script[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?script[\s]*?>";//定義style的正則表達(dá)式{或
String regEx_style = "<[\s]*?style[^>]*?>[\s\S]*?<[\s]*?\/[\s]*?style[\s]*?>";//定義HTML標(biāo)簽的正則表達(dá)式
String regEx_html = "<[^>]+>";//定義一些特殊字符的正則表達(dá)式 如:?????
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標(biāo)簽
p_style =Pattern.compile(regEx_style, Pattern.CASE_INSENSITIVE);
m_style=p_style.matcher(htmlStr);
htmlStr= m_style.replaceAll(""); //過濾style標(biāo)簽
p_html =Pattern.compile(regEx_html, Pattern.CASE_INSENSITIVE);
m_html=p_html.matcher(htmlStr);
htmlStr= m_html.replaceAll(""); //過濾html標(biāo)簽
p_special =Pattern.compile(regEx_special, Pattern.CASE_INSENSITIVE);
m_special=p_special.matcher(htmlStr);
htmlStr= m_special.replaceAll(""); //過濾特殊標(biāo)簽
textStr =htmlStr;
}catch(Exception e) {
e.printStackTrace();
}return textStr;//返回文本字符串
}/*** 測試用的main函數(shù)
*@paramargs*/
public static voidmain(String[] args) {
StringBuffer sb= newStringBuffer();try{
FileReader fr= new FileReader("D:/test.html");
BufferedReader br= newBufferedReader(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);
}
}
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎勵來咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎總結(jié)
以上是生活随笔為你收集整理的java正则匹配非html字符串_java正则表达式去除html中所有的标签和特殊HTML字符(以开头的)...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: css 透明度_如何使用CSS实现精美视
- 下一篇: 怎么改变表单option标签直接字体大小