c#中過濾html的正則表達(dá)式
實現(xiàn)代碼
/// <summary>
/// 去除HTML標(biāo)記
/// </summary>
/// <param name=”NoHTML”>包括HTML的源碼 </param>
/// <returns>已經(jīng)去除后的文字</returns>
public static string NoHTML(string Htmlstring)
{
//刪除腳本
Htmlstring = Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "",
RegexOptions.IgnoreCase);
//刪除HTML
Htmlstring = Regex.Replace(Htmlstring, @"<(.[^>]*)>", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"([\r\n])[\s]+", "",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"–>", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"<!–.*", "", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(quot|#34);", "\"",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(amp|#38);", "&",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(lt|#60);", "<",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(gt|#62);", ">",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ",
RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(iexcl|#161);", "\xa1", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(cent|#162);", "\xa2", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(pound|#163);", "\xa3", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&(copy|#169);", "\xa9", RegexOptions.IgnoreCase);
Htmlstring = Regex.Replace(Htmlstring, @"&#(\d+);", "", RegexOptions.IgnoreCase);
Htmlstring.Replace("<", "");
Htmlstring.Replace(">", "");
Htmlstring.Replace("\r\n", "");
Htmlstring = HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
return Htmlstring;
}
C#過濾Html標(biāo)簽及空格
public static string FilterHTML(string HTMLStr)
{
if (!string.IsNullOrEmpty(HTMLStr))
return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>| ", "");
else
return "";
}
寫一個靜態(tài)方法移除HTML標(biāo)簽
#region
/// <summary>
/// 移除HTML標(biāo)簽
/// </summary>
/// <param name="HTMLStr">HTMLStr</param>
public static string ParseTags(string HTMLStr)
{
return System.Text.RegularExpressions.Regex.Replace(HTMLStr, "<[^>]*>", "");
}
#endregion
取出文本中的圖片地址
#region
/// <summary>
/// 取出文本中的圖片地址
/// </summary>
/// <param name="HTMLStr">HTMLStr</param>
public static string GetImgUrl(string HTMLStr)
{
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;
}
#endregion
提取HTML代碼中文字的C#函數(shù)
/// <summary>
/// 提取HTML代碼中文字的C#函數(shù)
/// </summary>
/// <param name="strHtml">包括HTML的源碼 </param>
/// <returns>已經(jīng)去除后的文字</returns>
using System;
using System.Text.RegularExpressions;
public class StripHTMLTest
{
public static void Main()
{
string s = StripHTML(
"<HTML><HEAD><TITLE>中國石龍信息平臺</TITLE></HEAD><BODY>faddfs龍信息平臺</BODY></HTML>");
Console.WriteLine(s);
}
public static string StripHTML(string strHtml)
{
string[]aryReg =
{
@"<script[^>]*?>.*?</script>",
@"<(\/\s*)?!?((\w+:)?\w+)(\w+(\s*=?\s*(([""'])(\\["
"'tbnr]|[^\7])*?\7|\w+)|.{0})|\s)*?(\/\s*)?>", @"([\r\n])[\s]+", @
"&(quot|#34);", @"&(amp|#38);", @"&(lt|#60);", @"&(gt|#62);", @
"&(nbsp|#160);", @"&(iexcl|#161);", @"&(cent|#162);", @"&(pound|#163);",
@"&(copy|#169);", @"&#(\d+);", @"-->", @"<!--.*\n"
};
string[]aryRep =
{
"", "", "", "\"", "&", "<", ">", " ", "\xa1", //chr(161),
"\xa2", //chr(162),
"\xa3", //chr(163),
"\xa9", //chr(169),
"", "\r\n", ""
};
string newReg = aryReg[0];
string strOutput = strHtml;
for (int i = 0; i < aryReg.Length; i++)
{
Regex regex = new Regex(aryReg[i], RegexOptions.IgnoreCase);
strOutput = regex.Replace(strOutput, aryRep[i]);
}
strOutput.Replace("<", "");
strOutput.Replace(">", "");
strOutput.Replace("\r\n", "");
return strOutput;
}
}
TempContent 表示包含有html的字符串;
TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"<[^>]+>","");至少一個
TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"<[^>]*>","");任意個
- C#使用正則表達(dá)式過濾html標(biāo)簽
- C#基于正則表達(dá)式抓取a標(biāo)簽鏈接和innerhtml的方法
- C#正則表達(dá)式匹配HTML中的圖片路徑,圖片地址代碼
- 常用正則 常用的C#正則表達(dá)式
- C#正則表達(dá)式使用方法示例
- c#匹配整數(shù)和小數(shù)的正則表達(dá)式
- C#使用正則表達(dá)式實例
- C#的正則表達(dá)式Regex類使用簡明教程
- c#使用正則表達(dá)式匹配字符串驗證URL示例
- c#判斷字符是否為中文的三種方法分享(正則表達(dá)式判斷)
- C# 正則表達(dá)式經(jīng)典分類整理集合手冊
- C#正則過濾HTML標(biāo)簽并保留指定標(biāo)簽的方法
相關(guān)文章
在ASP.NET 2.0中操作數(shù)據(jù)之五十九:使用SQL緩存依賴項SqlCacheDependency
當(dāng)緩存數(shù)據(jù)庫數(shù)據(jù)時,最理想的狀態(tài)是數(shù)據(jù)一直駐留在內(nèi)存,直到數(shù)據(jù)庫發(fā)生了改動。在ASP.NET 2.0,可以通過編程或聲明代碼的方式使用SQL cache dependencies,配合SQL Server 2005來實現(xiàn)此功能。2016-05-05
在ASP.NET 2.0中操作數(shù)據(jù)之十三:在DetailsView控件中使用TemplateField
就像在GridView中那樣,DetailsView控件也可以同樣的使用TemplateField。本文用兩個TemplateField來演示在它的使用方法。2016-05-05
在ASP.NET 2.0中操作數(shù)據(jù)之六十三:GridView實現(xiàn)批量刪除數(shù)據(jù)
本文主要介紹在GridView控件中包含一個checkbox列來實現(xiàn)復(fù)選多條數(shù)據(jù),在批量刪除按鈕的事件中通過for循環(huán)來一一刪除。2016-05-05
為Visual Studio手工安裝微軟ReportViewer控件
這篇文章介紹了為Visual Studio手工安裝微軟ReportViewer控件的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
在ASP.NET 2.0中操作數(shù)據(jù)之十:使用 GridView和DetailView實現(xiàn)的主/從報表
本文我們主要研究了如何使用可選擇行的GridView顯示主記錄,以及在DetailsView中顯示選中記錄的詳細(xì)信息。2016-05-05
解讀ASP.NET 5 & MVC6系列教程(8):Session與Caching
這篇文章主要介紹了ASP.NET 5 中的Session與Caching配置和使用,需要的朋友可以參考下2016-06-06
在ASP.NET 2.0中操作數(shù)據(jù)之二十六:排序自定義分頁數(shù)據(jù)
前面教程中我們實現(xiàn)了高效的自定義分頁,但是只是一種固定排序方式。在本教程中,我們通過引入@sortExpression來擴(kuò)展存儲過程既實現(xiàn)自定義分頁又提供自定義排序的功能。2016-05-05
在ASP.NET 2.0中操作數(shù)據(jù)之四十:自定義DataList編輯界面
先前的編輯界面清一色的都是TextBox控件,當(dāng)然EditItemTemplate還可以包含很多其他的控件,比如DropDownLists, RadioButtonLists, Calendars等,這一節(jié)就讓我們看看如何實現(xiàn)添加其他的控件。2016-05-05
在ASP.NET 2.0中操作數(shù)據(jù)之六十七:在TableAdapters中使用JOINs
使用TableAdapter向?qū)в幸欢ǖ木窒扌?,只能?chuàng)建出不含JOIN的存儲過程,那么本文就為大家講解,如何在TableAdapters中使用包含JOIN的存儲過程。2016-05-05

