我在文本文件中有以下类型的数据:
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!
}
假设我想找到关键字"累积所得税"。 如果找到,我想打印此关键字下的所有行,直到换行符。
这是我的解决方案:
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;
}
}
}
希望这有帮助。
这是解决此问题的另一种方法,使用正则表达式并一次读取整个文件(而不是逐行):
//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]);
}
}
正则表达式 101 中的示例。
基本上,您创建两个捕获组:一个用于关键字,一个用于描述。关键字后面必须有一个.
,也许是一个或多个空格字符(.+?
)和一个换行符(n
)。在此之后,主组捕获描述,直到最后一个点((.*\.)
)。