简体   繁体   中英

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

I've got a StreamReader which is reading lines from a file.

    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

How do I stop adding lines to a List if a line contains a regex pattern? For example, In my code below, I've got a regex patter which contains the word "Grid".I want to add each line before the first occurrence of the word "Grid" to the list and I would like it to stop adding items to the list once it finds the word "Grid". So the list called HeaderParse<> should only contain the lines:

    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]

Here is the code I am using:

       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 was designed exactly for that. So just add.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM