读取文本文件中的多个特定行数

时间:2013-12-29 16:52:51

标签: c# text while-loop file.readalllines

我在文本文件中有以下类型的数据:

     Accumulated Earnings Tax. 
     A tax penalty which is imposed on corporate earnings which are retained by the corporation for non-
     business related needs. 

     Acquisition Cost. 
     The expenses incurred by an insurer or reinsurance company that are directly related to putting the 
     business on the books of the company. The largest portion of this cost is usually the agent's or sales 
     representative's commission or bonus. 

     Act of God. 
     An event arising out of natural causes with no human intervention which could not have been prevented 
     by reasonable care or foresight. Examples are floods, lightning, and earthquakes. 

     Actual Cash Value. 
     An amount equivalent to the replacement cost of lost or damaged property at the time of the loss, less 
     depreciation. With regard to buildings, there is a tendency for the actual cash value to closely parallel the 
     market value of the property. See also Market Value. 

每个带描述的新单词都有换行符!现在我要做的是阅读文件搜索关键字并打印其描述。关键字下面的行,但是在换行符标记的下一个单词之前。

我开发了一个代码来查找关键字并打印其描述,但它只打印了一行描述!

我的代码:

    int count = 1;
    private void openfile_Click(object sender, EventArgs e)
    {
        if (text.Text == String.Empty)
        {
            err.SetError(text, "Needs to contain Text");
        }

        DialogResult result = open_dialog.ShowDialog();

        if (result == DialogResult.OK)
        {

            try
            {
                string file_name = open_dialog.FileName;
                String lines_of_words;


                using (var file = System.IO.File.OpenText(file_name))
                {
                     // read each line, ensuring not null (EOF)
                    while ((lines_of_words = file.ReadLine()) != null)
                    {
                        if (lines_of_words.StartsWith(text.Text))
                        {

                            Console.WriteLine("TEXT IS YES"+count);
                            goGetDesc(file_name);
                            break;
                        }
                        else
                        {
                            count += 1;

                        }
                    }

                }
            }
            catch (Exception ex)
            {
            }
        }

    }

    private void goGetDesc(String file_name)
    {
        string[] lines = File.ReadAllLines(file_name);
        desc.Text+=(lines[count])+Environment.NewLine; //here i want to print the multiple lines if keyword has multiple lines!

    }

假设我想找到关键字“累计收入税”,如果找到我想要打印此关键字下的所有行,直到LINE BREAK。

2 个答案:

答案 0 :(得分:2)

这是我的解决方案:

using (StreamReader reader = File.OpenText("some file name"))
{
    string line;
    while ((line = reader.ReadLine()) != null)
    {
        if (line.StartsWith(textBoxInput.Text, StringComparison.OrdinalIgnoreCase))
        {
            // At this point we've already read the keyword and it matches our input
            StringBuilder description = new StringBuilder(512);
            string descLine;
            // Here we start reading description lines after the keyword.
            // Because every keyword with description is separated by blank line
            // we continue reading the file until, the last read line is empty 
            // (separator between keywords) or its null (eof)
            while ((descLine = reader.ReadLine()) != string.Empty && descLine != null)
            {
                description.AppendLine(descLine);
            }
            textBoxDescription.Text = description.ToString();
            break;
        }
    }
}

希望这有帮助。

答案 1 :(得分:1)

这是另一种解决方法,使用正则表达式并一次读取整个文件(不是逐行):

//using System.Text.RegularExpressions;

//the keyword you're looking for
String keyword = "Accumulated Earnings Tax";

//opening the file
using (StreamReader stream = File.OpenText("yourfile.txt"))
{
    //grabbing the whole content of the file at once
    String content = stream.ReadToEnd();                               

    //the regular expression, also see the regex101-link
    Regex re = new Regex("(" + keyword + "\\.).+?\n(.*\\.)");
    Match match = re.Match(content);
    if (match.Success)
    {
        //if the expression matches we can access our capturing groups
        //and extract the keyword and description
        Console.WriteLine("Keyword: " + match.Groups[1]);
        Console.WriteLine("Description: " + match.Groups[2]);
    }
}

regex101处的示例 基本上你创建了两个捕获组:一个用于关键字,一个用于描述。关键字后面必须有.,可能是一个或多个空白字符(.+?)和换行符(\n)。在此之后,maingroup捕获描述直到最后一个点((.*\\.))。