• 爱情文章
  • 亲情文章
  • 友情文章
  • 生活随笔
  • 校园文章
  • 经典文章
  • 人生哲理
  • 励志文章
  • 搞笑文章
  • 心情日记
  • 英语文章
  • 范文大全
  • 作文大全
  • 新闻阅读
  • 当前位置: 山茶花美文网 > 亲情文章 > 正文

    用C#过滤HTML代码的函数|用c语言编写的代码程序

    时间:2018-08-11来源:山茶花美文网 本文已影响 山茶花美文网手机站

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

    以下是引用片段:
    publicstringcheckStr(stringhtml)
    {
    System.Text.RegularExpressions.Regexregex1=newSystem.Text.RegularExpressions.Regex(@"<script[sS]+</script*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    System.Text.RegularExpressions.Regexregex2=newSystem.Text.RegularExpressions.Regex(@"href*=*[sS]*script*:",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    System.Text.RegularExpressions.Regexregex3=newSystem.Text.RegularExpressions.Regex(@"no[sS]*=",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    System.Text.RegularExpressions.Regexregex4=newSystem.Text.RegularExpressions.Regex(@"<iframe[sS]+</iframe*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    System.Text.RegularExpressions.Regexregex5=newSystem.Text.RegularExpressions.Regex(@"<frameset[sS]+</frameset*>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    System.Text.RegularExpressions.Regexregex6=newSystem.Text.RegularExpressions.Regex(@"<img[^>]+>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    System.Text.RegularExpressions.Regexregex7=newSystem.Text.RegularExpressions.Regex(@"</p>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    System.Text.RegularExpressions.Regexregex8=newSystem.Text.RegularExpressions.Regex(@"<p>",System.Text.RegularExpressions.RegexOptions.IgnoreCase);
    System.Text.RegularExpressions.Regexregex9=newSystem.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>","");
    returnhtml;
    }

    • 用C#过滤HTML代码的函数|用c语言编写的代码程序 相关文章:
    • 爱情文章
    • 亲情文章
    • 友情文章
    • 随笔
    • 哲理
    • 励志
    • 范文大全