一、
public static String getText(String htmlStr) {
if(htmlStr==null || "".equals(htmlStr)) return "";
String textStr ="";
java.util.regex.Pattern pattern;
java.util.regex.Matcher matcher;
try {
String regEx_remark = "<!--.+?-->";
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 = "<[^>]+";
htmlStr = htmlStr.replaceAll("\n","");
htmlStr = htmlStr.replaceAll("\t","");
pattern=Pattern.compile(regEx_remark);//过滤注释标签
matcher=pattern.matcher(htmlStr);
htmlStr=matcher.replaceAll("") ;
pattern = Pattern.compile(regEx_script,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤script标签
pattern = Pattern.compile(regEx_style,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤style标签
pattern = Pattern.compile(regEx_html,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤html标签
pattern = Pattern.compile(regEx_html1,Pattern.CASE_INSENSITIVE);
matcher = pattern.matcher(htmlStr);
htmlStr = matcher.replaceAll(""); //过滤html标签
textStr = htmlStr.trim();
}catch(Exception e) {
System.out.println("获取HTML中的text出错:");
e.printStackTrace();
}
return textStr;//返回文本字符串
}
二、
public static string NoHTML(string Htmlstring)
{
//删除脚本
Htmlstring = Regex.Replace(Htmlstring,@"<script[^>]*?>.*?</script>","",RegexOptions.IgnoreCase);
//删除HTML
Htmlstring = Regex.Replace(Htmlstring,@"<(.[^>]*)>","",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"([\r\n])[\s]+","",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"-->","",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"<!--.*","",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(quot|#34);","\"",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(amp|#38);","&",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(lt|#60);","<",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(gt|#62);",">",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(nbsp|#160);"," ",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(iexcl|#161);","\xa1",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(cent|#162);","\xa2",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(pound|#163);","\xa3",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring,@"&(copy|#169);","\xa9",RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);","",RegexOptions.IgnoreCase);
Htmlstring.Replace("<","");
Htmlstring.Replace(">","");
Htmlstring.Replace("\r\n","");
//Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}