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;

  • 相关阅读:
    11月2日考试 题解(前缀和+哈希+树状数组+树链剖分)
    【Ynoi2016】谁的梦 题解(容斥+STL)
    ENABLE_DDL_LOGGING参数
    ORA-12012: error on auto execute of job 25;ORA-12005: may not schedule automatic refresh for times in the past
    Oracle增加数据文件
    使用Loop循环向测试表插入数据
    Oracle中开窗函数row_number()、rank()、dense_rank()
    安装Oracle 19c RAC创建ssh连接成功,测试报错INS-06006
    .Net5 WebApi Swagger 发布IIS遇到的坑
    .Net5 开启JWT认证授权
  • 原文地址:https://www.cnblogs.com/yeye518/p/2231621.html
Copyright © 2011-2022 走看看