• C#过滤所有HTML代码的函数


    自己写的一段正则表达式,作用是删除 Page 里面Code 中的  HTML标签,这在做采集信息,消除其中的HTML很有用处,,欢迎大家收藏!

    public string checkStr(string html)
          {
              System.Text.RegularExpressions.Regex regex1 = new System.Text.RegularExpressions.Regex(@"<script[\s\S]+</script *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
              System.Text.RegularExpressions.Regex regex2 = new System.Text.RegularExpressions.Regex(@" href *= *[\s\S]*script *:", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
              System.Text.RegularExpressions.Regex regex3 = new System.Text.RegularExpressions.Regex(@" no[\s\S]*=", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
              System.Text.RegularExpressions.Regex regex4 = new System.Text.RegularExpressions.Regex(@"<iframe[\s\S]+</iframe *>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
              System.Text.RegularExpressions.Regex regex5 = new System.Text.RegularExpressions.Regex(@"<frameset[\s\S]+</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;
    }

  • 相关阅读:
    2019.04.24笔记
    转:浅谈SimpleDateFormat的线程安全问题
    H5和CSS
    Android Studio中 no module 问题,解决方法
    JS中,跨域调用(本地)另一个项目的方法
    IDEA中找不到spring的配置文件,或者不存在某个目录(比如没有src 目录)
    IDEA导入 Eclipse项目
    更新Android Studio,提示后直接点更新即可。gradle 两种更新方法,我用的第二种:手动添加gradle
    Android Studio的HAXM不支持虚拟机
    小米手机驱动程序安装教程
  • 原文地址:https://www.cnblogs.com/zzmzaizai/p/967731.html
Copyright © 2020-2023  润新知