1

我试过这段代码:

private void RemoveLines(List<string> Keywords)
        {
            string NewText = "";
            String regex = string.Empty;
            this.Invoke(new MethodInvoker(delegate { NewText = richTextBox1.Text; }));
            Regex MyRegex = null;
            foreach (string keyword in Keywords)
            {
                regex = String.Format(@"^.*\W{0}\W.*$",keyword);
                MyRegex = new Regex(regex, RegexOptions.Multiline);
                NewText = MyRegex.Replace(NewText, Environment.NewLine);
            }
            //Remove blank lines
            NewText = Regex.Replace(NewText, @"^\s+$[\r\n]*", "", RegexOptions.Multiline);
            this.Invoke(new MethodInvoker(delegate { richTextBox1.Text = NewText; }));
            this.Invoke(new MethodInvoker(delegate { richTextBox1.Refresh(); }));
        }

和 removeExtrenals 功能:

private List<string> removeExternals(List<string> externals)
        {
            if(!LocalyKeyWords.ContainsKey(mainUrl))
            {
                return externals;
            }
            List<string> keywords = LocalyKeyWords[mainUrl];
            List<int> indices = new List<int>();
            foreach(string keyword in keywords)
            {
                //Accumulate a list of the indices of the items that match.
                indices = indices.Concat(externals.Select((v, i) => v.Contains(keyword) ? i : -1)).ToList();
            }
            //Filter out the -1s, grab only the unique indices.
            indices = indices.Where(i => i >= 0).Distinct().ToList();
            //Filter out those items that match the keyword(s) related to mainUrl.
            externals = externals.Where((v, i) => !indices.Contains(i)).ToList();
            return externals;
        }

我在这个函数中调用了 removelines 和 removeexternals:

RemoveLines(removeExternals(webSites));

在使用断点后的 removeExternals 中,我看到两个不包含 google 的站点(示例中使用的关键字 im)所以从 18 个 url 我应该在richTextBox 中看到只有 16 个 url 我也在 removelines 函数中使用了断点,没有例外,但我看到了所有的网址都在richTextBox 中。

4

1 回答 1

1

试试这样:
我添加了一些随机数据进行测试..

private void Form1_Load(object sender, EventArgs e)
    {
        richTextBox1.AppendText(@"Loading The Url:   http://www.blogger.com/?tab=wj... Failed ");
        richTextBox1.AppendText(Environment.NewLine);
        richTextBox1.AppendText(@"Loading The Url:   http://www.google.co.il/intl/iw/options/... Done  ");
        richTextBox1.AppendText(Environment.NewLine);
        richTextBox1.AppendText(@"Loading The Url:   http://www.xyz.com/?tab=wj... Failed ");
        richTextBox1.AppendText(Environment.NewLine);
        richTextBox1.AppendText(@"Loading The Url:   http://www.abc.com/?tab=wj... Done ");
        richTextBox1.AppendText(Environment.NewLine);
        richTextBox1.AppendText(@"Loading The Url:   http://www.so.com/?tab=wj... Failed ");
        richTextBox1.AppendText(Environment.NewLine);
        keys.Add("xyz");
        keys.Add("abc");
        RemoveLines(keys);
    }  

这是方法的代码RemoveLines..

private void RemoveLines(List<string> Keywords)
    {
        String regex = string.Empty;
        string NewText = richTextBox1.Text;            
        Regex MyRegex = null;
        foreach (string keyword in Keywords)
        {
            regex = String.Format(@"^.*\W{0}\W.*$",keyword);
            MyRegex = new Regex(regex, RegexOptions.Multiline);
            NewText = MyRegex.Replace(NewText, Environment.NewLine);
        }
        //Remove blank lines
        NewText = Regex.Replace(NewText, @"^\s+$[\r\n]*", "", RegexOptions.Multiline); 
        richTextBox1.Text = NewText;
        richTextBox1.Refresh();
    }  

希望能帮助到你 !

于 2012-10-09T06:43:36.860 回答