【问题标题】:Search raw text from url, using keyword from textbox使用文本框中的关键字从 url 搜索原始文本
【发布时间】:2020-05-21 07:43:42
【问题描述】:

我和我的好友 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";
        }
    }
}

【问题讨论】:

  • 另外补充一下,它只读取原始文本的第一行。
  • 我已经回答了你,你的代码有多个问题,导致多个问题。如果您不能使用我的答案,我认为您需要让您的问题更加集中,否则我最终会为您编写明确的解决方案。

标签: c# wpf search


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    下面是工作代码sn-p:

     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");
            }
       }
    

    【讨论】:

      猜你喜欢
      • 2012-10-06
      • 2015-09-05
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多