一个用C#过滤HTML代码的函数

2016-02-19 13:47 28 1 收藏

下面是个超简单的一个用C#过滤HTML代码的函数教程,图老师小编精心挑选推荐,大家行行好,多给几个赞吧,小编吐血跪求~

【 tulaoshi.com - 编程语言 】

正好有时间所以用C#写了一段正则表达式,作用是删除 Page 里面Code 中的 HTML标签,这在做采集信息,消除其中的HTML很有用处。

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

以下是引用片段:
public string checkStr(string html)
      {
          System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"script[sS]+/script *", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
          System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[sS]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
          System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[sS]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
          System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"iframe[sS]+/iframe *", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
          System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"frameset[sS]+/frameset *", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
          System.Text.RegularExpressions.Regex regex6 = new System.Text.RegularExpressions.Regex(@"img[^]+", System.Text.RegularExpressions.RegexOptions.IgnoreCase); 
          System.Text.RegularExpressions.Regex regex7 = new System.Text.RegularExpressions.Regex(@"/p", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
          System.Text.RegularExpressions.Regex regex8 = new System.Text.RegularExpressions.Regex(@"p", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
          System.Text.RegularExpressions.Regex regex9 = new System.Text.RegularExpressions.Regex(@"[^]*", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
          html = regex1.Replace(html, ""); //过滤script/script标记
          html = regex2.Replace(html, ""); //过滤href=javascript: (A) 属性
          html = regex3.Replace(html, " _disibledevent="); //过滤其它控件的on...事件
          html = regex4.Replace(html, ""); //过滤iframe
          html = regex5.Replace(html, ""); //过滤frameset
          html = regex6.Replace(html, ""); //过滤frameset
          html = regex7.Replace(html, ""); //过滤frameset
          html = regex8.Replace(html, ""); //过滤frameset
          html = regex9.Replace(html, "");
          html = html.Replace(" ", "");
          html = html.Replace("/strong", "");
          html = html.Replace("strong", "");
          return html;
}

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

来源:https://www.tulaoshi.com/n/20160219/1605241.html

延伸阅读
using System;  using System.DirectoryServices;  using System.Collections;  using System.Text.RegularExpressions;  using System.Text;  /**   * @author 吴海燕   * @email  wuhy80-usual@yahoo.com   * 2004-...
ScreenSaver.cs using System;using System.Windows.Forms; namespace ScreenSaver{ public class DotNETScreenSaver { [STAThread] static void Main(string[] args) { if (args.Length > 0) { if (args[0].ToLower().Trim().Substring(0,2) == "/c") { MessageBox.Show("This Screen Saver has no options you can set."...
标签: Web开发
TempContent 表示包含有html的字符串; TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"[^]+","");至少一个 TempContent = System.Text.RegularExpressions.Regex.Replace(TempContent,"[^]*","");任意个 
标签: ASP
       作者:rainbow ASP动网先锋      3.0 选择一个编辑器      尽管我是一个顽固的Notepad狂,但这次我不建议用它编辑源码。原因是你正在与真正的编程语言打交道,使用Notepad编辑源码编译时可能产生大量的错误信息行(C++程序员知道我在说什么。)  &n...
其它方法见.用C#在本地创建一个Windows帐户(DirectoryServices)  http://yaosansi.com/blog/article.asp?id=917    在WinForm下该程序可写成:   (已测试通过,需要以Administrator身份运行)       using   System;       name...

经验教程

528

收藏

39
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部