zoukankan      html  css  js  c++  java
  • 解决C#识别变量定义的正则表达式

     Collection collection = new Collection();
                collection.Coding = "GB2312";
                string HtmlCode = collection.ReaderHTMLCode("http://www.qqtu88.com/qqjq/index.html");
                string pageurl = "http://www.qqtu88.com/qqjq/QQkongjianjishu/2011/0806/27479.html
    ";
                string pReg = "
    \\d+";
                string Link_str = "";
                Uri u = new Uri(pageurl);
                pageurl = pageurl.ToLower().Replace("http://", "").Replace(u.Host.ToString().ToLower(), "");
                pageurl = Regex.Replace(pageurl, pReg, "\\\\d+");
                pageurl = pageurl.Replace(".", "\\\\.");
                Label1.Text = pageurl;
                MatchCollection m = Regex.Matches(HtmlCode.ToLower(), Regex.(pageurl));
                for (int c = 0; c <= m.Count - 1; c++)
                {
                    string strNew = m[c].ToString();
                    // 过滤重复的URL 
                    if (Link_str.IndexOf(strNew) == -1)
                    {
                        Link_str += "http://" + u.Host + strNew + ",";
                    }
                }
                TextBox1.Text = Link_str;

    最主要的是Regex.这个函数可以对变量中的转义字符进行转义

    下面方法也可以

     Collection collection = new Collection();
                collection.Coding = "GB2312";
                string HtmlCode = collection.ReaderHTMLCode("http://www.qqtu88.com/qqjq/index.html");
                string pageurl = "http://www.qqtu88.com/qqjq/QQkongjianjishu/2011/0806/27479.html";
                string pReg = "\\d+";
                string Link_str = "";
                Uri u = new Uri(pageurl);
                pageurl = pageurl.ToLower().Replace("http://", "").Replace(u.Host.ToString().ToLower(), "");
                pageurl = Regex.Replace(pageurl, pReg, "\\d+");
                pageurl = pageurl.Replace(".", "\\.");
                Label1.Text = pageurl;
                Regex r = new Regex(pageurl , RegexOptions.IgnoreCase);
                MatchCollection m = r.Matches(HtmlCode.ToLower());
                for (int c = 0; c <= m.Count - 1; c++)
                {
                    string strNew = m[c].ToString();
                    // 过滤重复的URL 
                    if (Link_str.IndexOf(strNew) == -1)
                    {
                        Link_str += "http://" + u.Host + strNew + ",";
                    }
                }
                TextBox1.Text = Link_str;

  • 相关阅读:
    Tomcat临时目录及java.io.tmpdir对应的目录
    第一篇随笔
    面试
    近期小结-082714
    从头开始构建web前端应用——字符炸弹小游戏(一)
    web版ppt制作插件impress.js源码注释翻译
    .net web service Application_BeginRequest,记录请求数据
    微信APP支付,阿里云服务器,统一下单请求超时
    android仿ios圆弧边框背景
    google map 地址编码及反向地址编码
  • 原文地址:https://www.cnblogs.com/yeye518/p/2231621.html
Copyright © 2011-2022 走看看