最近作的一个东东,想要去除掉保存的字符串内容的全部html格式,也就是只取纯文本,html
从网上查了些相关资料,以及正则表达式的用法,正则表达式
有一个方法向你们推荐,基本去除了我想要去除的html格式,可能根据须要不一样,还须要你本身编写相关正则匹配,这里我本身加了一个去除全部空格的匹配,网络
下面把这个提取纯文本的方法跟贴出来跟你们学习,请多多指点。ide
参阅网络资料:http://www.suzhou35.com/blog/article.asp?id=169post
一、[用正则表达式来提取html中的文本]学习
须要引用的命名空间是System.Text.RegularExpressionsurl
using System.Text.RegularExpressions;spa
思路:htm
a、先将html文本中的全部空格、换行符去掉(由于html中的空格和换行是被忽略的)
b、将<head>标记中的全部内容去掉
c、将<script>标记中的全部内容去掉
d、将<style>标记中的全部内容去掉
e、将td换成空格,tr,li,br,p 等标记换成换行符
f、去掉全部以“<>”符号为头尾的标记去掉。
g、转换&,&nbps;等转义字符换成相应的符号
h、去掉多余的空格和空行
blog
方法以下:
/// <summary>
/// 将HTML内容转换成纯文本形式,即去除HTML格式
/// </summary>
/// <param name="source"></param>
/// <returns></returns>
public static string ConvertToText(string source)
{
string result; //remove line breaks,tabs
result = source.Replace("\r", " ");
result = result.Replace("\n", " ");
result = result.Replace("\t", " ");
//remove the header
result = Regex.Replace(result, "(<head>).*(</head>)", string.Empty, RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"<( )*script([^>])*>", "<script>", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"(<script>).*(</script>)", string.Empty, RegexOptions.IgnoreCase);
//remove all styles
result = Regex.Replace(result, @"<( )*style([^>])*>", "<style>", RegexOptions.IgnoreCase); //clearing attributes
result = Regex.Replace(result, "(<style>).*(</style>)", string.Empty, RegexOptions.IgnoreCase);
//insert tabs in spaces of <td> tags
result = Regex.Replace(result, @"<( )*td([^>])*>", " ", RegexOptions.IgnoreCase);
//insert line breaks in places of <br> and <li> tags
result = Regex.Replace(result, @"<( )*br( )*>", "\r", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"<( )*li( )*>", "\r", RegexOptions.IgnoreCase);
//insert line paragraphs in places of <tr> and <p> tags
result = Regex.Replace(result, @"<( )*tr([^>])*>", "\r\r", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"<( )*p([^>])*>", "\r\r", RegexOptions.IgnoreCase);
//remove anything thats enclosed inside < >
result = Regex.Replace(result, @"<[^>]*>", string.Empty, RegexOptions.IgnoreCase);
//replace special characters:
result = Regex.Replace(result, @"&", "&", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @" ", " ", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"<", "<", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @">", ">", RegexOptions.IgnoreCase);
result = Regex.Replace(result, @"&(.{2,6});", string.Empty, RegexOptions.IgnoreCase);
//remove extra line breaks and tabs
result = Regex.Replace(result, @" ( )+", " ");
result = Regex.Replace(result, "(\r)( )+(\r)", "\r\r");
result = Regex.Replace(result, @"(\r\r)+", "\r\n");
//remove blank
result = Regex.Replace(result, @"\s", "");
return result;
}
二、[取出文本中的图片地址]
程序代码
{
string str = string .Empty ;
string sPattern = @ "^<img\s+[^>]*>" ;
Regex r = new Regex (@ "<img\s+[^>]*\s*src\s*=\s*([']?)(?<url>\S+)'?[^>]*>" ,
RegexOptions .Compiled ) ;
Match m = r .Match (HTMLStr .ToLower ( ) ) ;
if (m .Success )
str = m .Result ( "${url}" ) ;
return str ;
}