簡體   English   中英

停止添加到列表 <String> 當行與正則表達式匹配時-C#

[英]Stop adding to List<String> when line matches regex - C#

我有一個StreamReader,它正在從文件中讀取行。

    Tyndall Centre grim file created on 22.01.2004 at 17:57 by Dr. Tim Mitchell
.pre = precipitation (mm)
CRU TS 2.1
[Long=-180.00, 180.00] [Lati= -90.00,  90.00] [Grid X,Y= 720, 360]
[Boxes=   67420] [Years=1991-2000] [Multi=    0.1000] [Missing=-999]
Grid-ref=   1, 148
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
 3020 2820 3040 2880 1740 1360  980  990 1410 1770 2580 2630
Grid-ref=   1, 311
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
  490  290  280  230  200  250  440  530  460  420  530  450
Grid-ref=   1, 312
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410
  460  280  260  220  190  240  430  520  450  400  520  410

如果行包含正則表達式模式,如何停止向列表中添加行? 例如,在下面的代碼中,我有一個包含單詞“ Grid”的正則表達式。我想將單詞“ Grid”的第一次出現之前的每一行添加到列表中,我希望它停止添加找到單詞“ Grid”后,將其添加到列表中。 因此,名為HeaderParse <>的列表應僅包含以下行:

    Tyndall Centre grim file created on 22.01.2004 at 17:57 by Dr. Tim Mitchell
.pre = precipitation (mm)
CRU TS 2.1
[Long=-180.00, 180.00] [Lati= -90.00,  90.00] [Grid X,Y= 720, 360]
[Boxes=   67420] [Years=1991-2000] [Multi=    0.1000] [Missing=-999]

這是我正在使用的代碼:

       private void button1_Click(object sender, EventArgs e)
    {

        StreamReader reader = File.OpenText("cru-ts-2-10.1991-2000-cutdown.pre");

        string line;

        var regex = new Regex(@"(Grid)");

        List<String> HeaderParse = new List<string>();

        while ((line = reader.ReadLine()) != null)
        {
            if (!regex.IsMatch(line))
            {
                HeaderParse.Add(line);
            }
            else
            {
 //stop it adding stuff here
            }
        }
        MessageBox.Show("This button has been clicked");
    }
 while ((line = reader.ReadLine()) != null)
    {
        if (!regex.IsMatch(line))
        {
            HeaderParse.Add(line);
        }
        else
        {
          //stop it adding stuff here
          break; 
        }
    }
    private void button1_Click(object sender, EventArgs e)
    {

        StreamReader reader = File.OpenText("cru-ts-2-10.1991-2000-cutdown.pre");

        string line;

        var regex = new Regex(@"(Grid)");

        List<String> HeaderParse = new List<string>();

        while ((line = reader.ReadLine()) != null && !regex.IsMatch(line))
        {
             HeaderParse.Add(line);

        }
        MessageBox.Show("This button has been clicked");
    }

Break正是為此目的而設計的。 因此,只需添加。

private void button1_Click(object sender, EventArgs e)
{

    StreamReader reader = File.OpenText("cru-ts-2-10.1991-2000-cutdown.pre");

    string line;

    var regex = new Regex(@"(Grid)");

    List<String> HeaderParse = new List<string>();

    while ((line = reader.ReadLine()) != null)
    {
        if (!regex.IsMatch(line))
        {
            HeaderParse.Add(line);
        }
        else
        {
            //stop it adding stuff here
            break;
        }
    }
    MessageBox.Show("This button has been clicked");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM