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)文章
NopCommerce架構(gòu)分析之(三)EntityFramework數(shù)據(jù)庫初試化及數(shù)據(jù)操作
本文介紹IStartupTask,該類會在系統(tǒng)啟動時執(zhí)行,IStartupTask調(diào)用IEfDataProvider進(jìn)行數(shù)據(jù)庫的初始化。2016-04-04
為Visual Studio手工安裝微軟ReportViewer控件
這篇文章介紹了為Visual Studio手工安裝微軟ReportViewer控件的方法,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-06-06
解讀ASP.NET 5 & MVC6系列教程(15):MvcOptions配置
這篇文章主要介紹了ASP.NET 5 MVC6中MvcOptions配置方法,需要的朋友可以參考下2016-06-06
.Net?6中WebApplicationBuilder介紹和用法
這篇文章介紹了.Net?6中WebApplicationBuilder的用法,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-12-12
在ASP.NET 2.0中操作數(shù)據(jù)之十一:基于數(shù)據(jù)的自定義格式化
GridView, DetailsView, FormView的格式自定義可以有多種方法, 在本文中我們將用DataBound 和 RowDataBound兩種事件來完成,下面主要演示了貨幣、加粗、斜體、高亮的數(shù)據(jù)格式化。2016-05-05
在ASP.NET 2.0中操作數(shù)據(jù)之五十一:從GridView的頁腳插入新記錄
本文介紹在ASP.NET 2.0中如何在GridView的頁腳動態(tài)插入一行新記錄,要顯示頁腳行只需要設(shè)置ShowFooter屬性為true。我們可以這樣對頁腳行進(jìn)行用戶定制:將每一列轉(zhuǎn)換成TemplateField,并在其FooterTemplate模板定制插入界面。2016-05-05
NopCommerce架構(gòu)分析之(六)自定義RazorViewEngine和WebViewPage
本文對NopCommerce的后臺分離技術(shù)做簡單的探討。NopCommerce通過自定義視圖引擎,重寫了VirtualPathProviderViewEngine類的CreateView、CreatePartialView、FindView、FindPartialView方法,添加自定義的視圖搜索路徑來實現(xiàn)后臺分離。2016-04-04

