C# 如何在txt文档中搜索单词,并将该单词写入新文档以进行报告?

C# 如何在txt文档中搜索单词,并将该单词写入新文档以进行报告?,c#,word,text-files,C#,Word,Text Files,我试图通过日志.txt文件查找错误消息的所有实例,然后让代码使用C#将该错误行粘贴到新文档中,并创建一个单独的.txt文件,其中只列出日志文件中的错误行 我知道如何使用C#搜索文档中的文本,但是提取这些错误消息(每个错误消息通常不超过1-2行)的最佳方法是什么,而不在第一个错误实例之后追加文档的整个其余部分 编辑 日志文件记录每行上的事件,错误行的读取方式如下: Running install update (3) ERROR: Running install update(3) (ERROR

我试图通过日志
.txt
文件查找错误消息的所有实例,然后让代码使用C#将该错误行粘贴到新文档中,并创建一个单独的
.txt
文件,其中只列出日志文件中的错误行

我知道如何使用C#搜索文档中的文本,但是提取这些错误消息(每个错误消息通常不超过1-2行)的最佳方法是什么,而不在第一个错误实例之后追加文档的整个其余部分

编辑

日志文件记录每行上的事件,错误行的读取方式如下:

Running install update (3)
ERROR: Running install update(3) (ERROR x34293) The system was unable to update the application, check that correct version is accessible by the system.
Running install update (4)
等等

如果有帮助,请告诉我。

类似于:

foreach(var line in File.ReadLines(filename)
                        .Where(l => l.Contains("ERROR MESSAGE")))
{
    // Log line
}
此外,如果您需要行内的特定信息,您可以使用来捕获信息。如果没有更多信息,我无法提供更好的示例。

类似于:

foreach(var line in File.ReadLines(filename)
                        .Where(l => l.Contains("ERROR MESSAGE")))
{
    // Log line
}

此外,如果您需要行内的特定信息,您可以使用来捕获信息。如果没有更多信息,我无法提供更好的示例。

您可以通过正则表达式模式搜索该文件,该模式提供找到的字符位置或行号(不记得)。然后可以获取从正则表达式返回的部分

下面是我自己编写的使用正则表达式的“查找并替换”程序中的一段代码。有一些嵌套的方法,但你明白了

int foundInThisFile;
string regExPattern = FindText;
System.Text.RegularExpressions.Regex regSearch = null;

if (IgnoreCase)
    regSearch = new System.Text.RegularExpressions.Regex(regExPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
else
    regSearch = new System.Text.RegularExpressions.Regex(regExPattern, System.Text.RegularExpressions.RegexOptions.Multiline);

            System.Text.RegularExpressions.MatchCollection regExMatches = regSearch.Matches(reader.ReadToEnd());

            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            found += regExMatches.Count;
            TotalMatches(new CountEventArgs(found));

            foundInThisFile = regExMatches.Count;
            MatchesInThisFile(new CountEventArgs(foundInThisFile));

            if (regExMatches.Count > 0)
            {
                foreach (System.Text.RegularExpressions.Match match in regExMatches)
                {
                    // The first "group" is going to be the entire regex match, any other "group" is going to be the %1, %2 values that are returned
                    // Index is the character position in the entire document
                    if (match.Groups.Count > 1)
                    {
                        // This means the user wants to see the grouping results
                        string[] groupsArray = new string[match.Groups.Count - 1];

                        for (int counter = 1; counter < match.Groups.Count; counter++)
                            groupsArray[counter - 1] = match.Groups[counter].Value;

                        int lineNumber = 0;
                        string actualLine = String.Empty;

                        GetLineNumberAndLine(localPath, match.Groups[0].Index, out lineNumber, out actualLine);

                        AddToSearchResults(localPath, lineNumber, actualLine, groupsArray);

                        NewSearchResult(new SearchResultArgs(new FindReplaceItem(localPath, lineNumber, actualLine, ConvertGroupsArrayToString(groupsArray))));
                    }
                    else
                    {
                        int lineNumber = 0;
                        string actualLine = String.Empty;

                        GetLineNumberAndLine(localPath, match.Groups[0].Index, out lineNumber, out actualLine);

                        AddToSearchResults(localPath, lineNumber, actualLine);

                        NewSearchResult(new SearchResultArgs(new FindReplaceItem(localPath, lineNumber, actualLine)));
                    }
                }
            }
int-foundthis文件;
字符串regexpatern=FindText;
System.Text.RegularExpressions.Regex regSearch=null;
如果(忽略案例)
regSearch=new System.Text.RegularExpressions.Regex(RegExparten,System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
其他的
regSearch=new System.Text.RegularExpressions.Regex(regexparten,System.Text.RegularExpressions.RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection regExMatches=regSearch.Matches(reader.ReadToEnd());
if(读卡器!=null)
{
reader.Dispose();
reader=null;
}
找到+=regExMatches.Count;
TotalMatches(新CountEventArgs(已找到));
foundInThisFile=regExMatches.Count;
匹配此文件(新CountEventArgs(foundInThisFile));
如果(regExMatches.Count>0)
{
foreach(regExMatches中的System.Text.RegularExpressions.Match)
{
//第一个“组”将是整个正则表达式匹配,任何其他“组”将是返回的%1,%2个值
//索引是整个文档中的字符位置
如果(match.Groups.Count>1)
{
//这意味着用户希望看到分组结果
string[]groupsArray=新字符串[match.Groups.Count-1];
对于(int counter=1;计数器
您可以通过正则表达式模式搜索文件,该模式提供找到的字符位置或行号(不记得)。然后可以获取从正则表达式返回的部分

下面是我自己编写的使用正则表达式的“查找并替换”程序中的一段代码。有一些嵌套的方法,但你明白了

int foundInThisFile;
string regExPattern = FindText;
System.Text.RegularExpressions.Regex regSearch = null;

if (IgnoreCase)
    regSearch = new System.Text.RegularExpressions.Regex(regExPattern, System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
else
    regSearch = new System.Text.RegularExpressions.Regex(regExPattern, System.Text.RegularExpressions.RegexOptions.Multiline);

            System.Text.RegularExpressions.MatchCollection regExMatches = regSearch.Matches(reader.ReadToEnd());

            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            found += regExMatches.Count;
            TotalMatches(new CountEventArgs(found));

            foundInThisFile = regExMatches.Count;
            MatchesInThisFile(new CountEventArgs(foundInThisFile));

            if (regExMatches.Count > 0)
            {
                foreach (System.Text.RegularExpressions.Match match in regExMatches)
                {
                    // The first "group" is going to be the entire regex match, any other "group" is going to be the %1, %2 values that are returned
                    // Index is the character position in the entire document
                    if (match.Groups.Count > 1)
                    {
                        // This means the user wants to see the grouping results
                        string[] groupsArray = new string[match.Groups.Count - 1];

                        for (int counter = 1; counter < match.Groups.Count; counter++)
                            groupsArray[counter - 1] = match.Groups[counter].Value;

                        int lineNumber = 0;
                        string actualLine = String.Empty;

                        GetLineNumberAndLine(localPath, match.Groups[0].Index, out lineNumber, out actualLine);

                        AddToSearchResults(localPath, lineNumber, actualLine, groupsArray);

                        NewSearchResult(new SearchResultArgs(new FindReplaceItem(localPath, lineNumber, actualLine, ConvertGroupsArrayToString(groupsArray))));
                    }
                    else
                    {
                        int lineNumber = 0;
                        string actualLine = String.Empty;

                        GetLineNumberAndLine(localPath, match.Groups[0].Index, out lineNumber, out actualLine);

                        AddToSearchResults(localPath, lineNumber, actualLine);

                        NewSearchResult(new SearchResultArgs(new FindReplaceItem(localPath, lineNumber, actualLine)));
                    }
                }
            }
int-foundthis文件;
字符串regexpatern=FindText;
System.Text.RegularExpressions.Regex regSearch=null;
如果(忽略案例)
regSearch=new System.Text.RegularExpressions.Regex(RegExparten,System.Text.RegularExpressions.RegexOptions.IgnoreCase | System.Text.RegularExpressions.RegexOptions.Multiline);
其他的
regSearch=new System.Text.RegularExpressions.Regex(regexparten,System.Text.RegularExpressions.RegexOptions.Multiline);
System.Text.RegularExpressions.MatchCollection regExMatches=regSearch.Matches(reader.ReadToEnd());
if(读卡器!=null)
{
reader.Dispose();
reader=null;
}
找到+=regExMatches.Count;
TotalMatches(新CountEventArgs(已找到));
foundInThisFile=regExMatches.Count;
匹配此文件(新CountEventArgs(foundInThisFile));
如果(regExMatches.Count>0)
{
foreach(regExMatches中的System.Text.RegularExpressions.Match)
{
//第一个“组”将是整个正则表达式匹配,任何其他“组”将是返回的%1,%2个值
//索引是整个文档中的字符位置
如果(match.Groups.Count>1)
{
//这意味着