-1

我和我的好友 Xylophone 已经在这个问题上待了几个小时,但无法弄清楚这一点,我们将不胜感激。我基本上是在尝试读取该 URL 中的所有文本并搜索关键字。

if (comboBoxEdit1.Text == "Hello")
{
    label2.Text = "Current Status: Searching...";
    this.dataGridView3.ScrollBars = ScrollBars.None;
    this.dataGridView3.MouseWheel += new MouseEventHandler(mousewheel);
    dataGridView3.Rows.Clear();
    string line;
    int row = 0;
    List<String> LinesFound = new List<string>();
    StreamReader file = new StreamReader("https://pastebin.com/raw/fWxKdRjN");
    while ((line = file.ReadLine()) != null)
    {
        if (line.Contains(textEdit1.Text))
        {
            string[] Columns = line.Split(':');
            dataGridView3.Rows.Add(line);
            for (int i = 0; i < Columns.Length; i++)
            {
                dataGridView3[i, row].Value = Columns[i];
            }
            row++;
            label2.Text = "Current Status: " + dataGridView3.Rows.Count + "  Matche(s) Found";
        }
        else if (dataGridView3.RowCount == 0)
        {
            label2.Text = "Current Status: No Matche(s) Found";
        }
    }
}
4

2 回答 2

0

我们可以use regular expression检查 URL 中是否存在“raw”。 Regex.Matches()函数将返回一个包含所有匹配项的数组。然后我们可以使用 count 属性来查找出现次数。

在 url 中匹配 raw 的正则表达式:(raw)

以下是工作代码片段:

 public static void Main()
   {
       string pattern = @"(raw)";
       Regex rgx = new Regex(pattern);
       string url = "https://pastebin.com/raw/fWxKdRjN";
       if (rgx.Matches(url).Count>0){
          Console.WriteLine(Current Status: " + rgx.Matches(url).Count + "  Matche(s) Found");
         }  
       else {
          Console.WriteLine("Current Status: No Matche(s) Found");
        }
   }
于 2020-05-21T08:52:32.393 回答
0

你做错了,如果你想阅读和解析网页的 html 内容,你需要使用 httpClient 获取页面,或者最好看看这个库https://html-agility-pack.net/

于 2020-05-21T08:31:21.743 回答