读书人

ASP.NET怎么去掉HTML字符串中的HTML标

发布时间: 2012-12-17 09:31:40 作者: rapoo

ASP.NET如何去掉HTML字符串中的HTML标签
有一段字符串类似“<OL><LI><STRONG>wwwwwww</STRONG><BR>wwwwww<BR><STRONG>wwwwwww</STRONG><BR><STRONG>
”这样的字符串,我想把这段字符串中的所有标签全部去掉,这剩下所有的wwwww,如何实现,大仙们给点指导
[最优解释]


StreamReader reader = new StreamReader("c:\\temp\\1.txt",Encoding.Default);
string source = reader.ReadToEnd();
Regex reg = new Regex(@"(?is)(?<=>)[^<]+(?=<)");
MatchCollection mc = reg.Matches(source);
foreach (Match m in mc)
{
MessageBox.Show(m.Value);
}

[其他解释]
http://blog.csdn.net/gulijiang2008/article/details/7190281
[其他解释]
这个用HtmlAgilityPack会非常简单。
[其他解释]

public static string ReplaceHtmlMark(string HtmlString)
{
string[] RegexString = {
@"style='.*?'",
@"class='.*?'",
@"<param.*?>(</param>)?",
@"<embed.*?>(</embed>)?",
@"<object.*?>(</object>)?",
@"<strong.*?>(</strong>)?",
@"<span.*?>(</span>)?",
@"<p.*?>(</p>)?",


@"<u.*?>(</u>)?",
@"<em.*?>(</em>)?",
//@"<div.*?>(</div>)?",

};
foreach (String str in RegexString)
{
Regex regex = new Regex(str, RegexOptions.IgnoreCase);
HtmlString = regex.Replace(HtmlString, string.Empty);
}
string[] RegexString2 = {
//@"</div>",
@"</p>",
@"</object>",
@"</strong>",
@"</span>",
@"</ins>",
};
foreach (String str2 in RegexString2)
{
Regex regex2 = new Regex(str2, RegexOptions.IgnoreCase);
HtmlString = regex2.Replace(HtmlString, string.Empty);


}
return HtmlString;
}


[其他解释]
Refer:
http://www.cnblogs.com/insus/archive/2012/04/25/2470430.html
[其他解释]
System.Text.RegularExpressions.Regex regex = new System.Text.RegularExpressions.Regex(@"<[^>]*>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string html = regex.Replace(html, "");

读书人网 >asp.net

热点推荐