使用正則表達式將Html轉換爲純文本

在網頁剛流行起來的時候,提取html中的文本有一個簡單的方法,就是將html文本(包含標記)中的所有以“<”符號開頭到以“>”符號之間的內容去掉即可。
但對於現在複雜的網頁而言,用這種方法提取出來的文本會有大量的空格、空行、script段落、還有一些html轉義字符,效果很差。
下面用正則表達式來提取html中的文本,
代碼的實現的思路是:
a、先將html文本中的所有空格、換行符去掉(因爲html中的空格和換行是被忽略的)
b、將<head>標記中的所有內容去掉
c、將<script>標記中的所有內容去掉
d、將<style>標記中的所有內容去掉
e、將td換成空格,tr,li,br,p 等標記換成換行符
f、去掉所有以“<>”符號爲頭尾的標記去掉。
g、轉換&amp;,&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, @"&amp;", "&", RegexOptions.IgnoreCase);
 result = Regex.Replace(result, @"&nbsp;", " ", RegexOptions.IgnoreCase);
 result = Regex.Replace(result, @"&lt;", "<", RegexOptions.IgnoreCase);
 result = Regex.Replace(result, @"&gt;", ">", 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章