使用正则表达式将Html转换为纯文本
来源:互联网 发布:下载类网站源码 编辑:程序博客网 时间:2024/05/17 09:15
在网页刚流行起来的时候,提取html中的文本有一个简单的方法,就是将html文本(包含标记)中的所有以“<”符号开头到以“>”符号之间的内容去掉即可。
但对于现在复杂的网页而言,用这种方法提取出来的文本会有大量的空格、空行、script段落、还有一些html转义字符,效果很差。
下面用正则表达式来提取html中的文本,
代码的实现的思路是:
a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)
b、将<head>标记中的所有内容去掉
c、将<script>标记中的所有内容去掉
d、将<style>标记中的所有内容去掉
e、将td换成空格,tr,li,br,p 等标记换成换行符
f、去掉所有以“<>”符号为头尾的标记去掉。
g、转换&,&nbps;等转义字符换成相应的符号
h、去掉多余的空格和空行
代码如下:
using System;
using System.Text.RegularExpressions;
namespace Kwanhong.Utilities
{
/// <summary>
/// HtmlToText 的摘要说明。
/// </summary>
public class HtmlToText
{
public string Convert(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");
return result;
}
}//end class
}//end namespace
using System.Text.RegularExpressions;
namespace Kwanhong.Utilities
{
/// <summary>
/// HtmlToText 的摘要说明。
/// </summary>
public class HtmlToText
{
public string Convert(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");
return result;
}
}//end class
}//end namespace
- 使用正则表达式将Html转换为纯文本
- 使用正则表达式将Html转换为纯文本
- 使用正则表达式将Html转换为纯文本
- Python 正则表达式将纯文本转化为HTML格式
- js将html转换为纯文本
- 如何将HTML转换为纯文本格式Text
- Java正则表达式提取html纯文本
- PHP html 转换为纯文本
- 将HTMLString转换为纯文本
- IOS使用正则表达式去掉html中的标签元素,获得纯文本
- IOS使用正则表达式去掉html中的标签元素,获得纯文本
- C#使用正则表达式将数字转换为大写中文
- 使用C#将HTML文本转换为普通文本,去掉所有的Html标记
- 使用C#将HTML文本转换为普通文本,去掉所有的Html标记
- 使用C#将HTML文本转换为普通文本,去掉所有的Html标记
- 使用C#将HTML文本转换为普通文本,去掉所有的Html标记
- 利用正则表达式除去html得到纯文本
- 将html格式的内容转换成纯文本格式
- 对索引选择的建议
- 通达网站
- 精妙SQL语句
- 关于原始输入--XP新技术(原创,转载请注明出处)
- use a relationship between two DataTable
- 使用正则表达式将Html转换为纯文本
- illustrates the use of a DataView object to filter and sort rows
- datagrid的数据分层
- Deleting Data Using SqlCommandBuilder
- 如何让搜索引擎收录我的站[转]
- NET Pet Shop 的设计模式与体系结构【引自微软】
- No Subject
- Deleting Data Using SQLStatements
- 中国古典养生术中的心理放松的方法