html 乱码转换为文字,将html格式的内容转换成纯文本格式-Go语言中文社区

最近做的一个东东,想要去除掉保存的字符串内容的所有html格式,也就是只取纯文本,

从网上查了些相关资料,以及正则表达式的用法,

有一个方法向大家推荐,基本去除了我想要去除的html格式,可能根据需要不同,还需要你自己编写相关正则匹配,这里我自己加了一个去除所有空格的匹配,

下面把这个提取纯文本的方法跟贴出来跟大家学习,请多多指点。

1、[用正则表达式来提取html中的文本]

需要引用的命名空间是System.Text.RegularExpressions

using System.Text.RegularExpressions;

思路:

a、先将html文本中的所有空格、换行符去掉(因为html中的空格和换行是被忽略的)

b、将

标记中的所有内容去掉

c、将

d、将

e、将td换成空格,tr,li,br,p 等标记换成换行符

f、去掉所有以“<>”符号为头尾的标记去掉。

g、转换&,&nbps;等转义字符换成相应的符号

h、去掉多余的空格和空行

方法如下:

///

/// 将HTML内容转换成纯文本形式,即去除HTML格式

///

///

///

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, "(

).*()", string.Empty, RegexOptions.IgnoreCase);

result = Regex.Replace(result, @"<( )*script([^>])*>", "

result = Regex.Replace(result, @"()", string.Empty, RegexOptions.IgnoreCase);

//remove all styles

result = Regex.Replace(result, @"])*>", "

result = Regex.Replace(result, "()", string.Empty, RegexOptions.IgnoreCase);

//insert tabs in spaces of

tags

result = Regex.Replace(result, @"])*>", " ", RegexOptions.IgnoreCase);

//insert line breaks in places of
and

tags

result = Regex.Replace(result, @"", "r", RegexOptions.IgnoreCase);

result = Regex.Replace(result, @"", "r", RegexOptions.IgnoreCase);

//insert line paragraphs in places of

and

tags

result = Regex.Replace(result, @"])*>", "rr", RegexOptions.IgnoreCase);

result = Regex.Replace(result, @"])*>", "rr", 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, @"

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)", "rr");

result = Regex.Replace(result, @"(rr)+", "rn");

//remove blank

result = Regex.Replace(result, @"s", "");

return result;

}

2、[取出文本中的图片地址]

code.gif 程序代码

public

static string GetImgUrl

(string HTMLStr

)

{

string str

= string

.Empty

;

string sPattern

= @

"^]*>"

;

Regex r

=

new Regex

(@

"]*s*srcs*=s*([']?)(?S+)'?[^>]*>"

,

RegexOptions

.Compiled

)

;

Match m

= r

.Match

(HTMLStr

.ToLower

(

)

)

;

if

(m

.Success

)

str

= m

.Result

(

"${url}"

)

;

return str

;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值