从单词中识别和提取(显示)错误

时间:2019-05-23 16:11:12

标签: c#

需要识别文本文件中的“错误”并将其显示在控制台中 正则表达式需要帮助,并且可以从文本中仅提取错误,因为它现在显示了全文。

我需要识别的

文本放在方括号中 现在付给我们2个比特币到: {18RJA5BpFe4CGDFQG59jLNhPqYCRaEFng1}

Regex finderror = new Regex(@"\W+\d{,15}");
DirectoryInfo id = new DirectoryInfo("E:\\student_copy\\text");
FileInfo[] fiArrrr = id.GetFiles();
foreach (FileInfo f in fiArrrr)
{
    string stuff = File.ReadAllText(f.FullName);
    Match errortext = finderror.Match(stuff);
    if (errortext.Success)
    {
        Console.WriteLine("");
    }
    else
    {
        Console.WriteLine("no error for file ",);
    }
}   

1 个答案:

答案 0 :(得分:0)

我使用了Regex Here from Milosz

您的代码,略有修改:

static void Main(string[] args)
{
    Regex finderror = new Regex(@"(?<=\{)[^}]*(?=\})");
    DirectoryInfo id = new DirectoryInfo("E:\\student_copy\\text");
    FileInfo[] fiArrrr = id.GetFiles();
    foreach (FileInfo f in fiArrrr)
    {
        Console.WriteLine("File: " + f.FullName);
        string stuff = File.ReadAllText(f.FullName);
        Match errortext = finderror.Match(stuff);
        if (errortext.Success)
        {
            Console.WriteLine("Error Found:");
            Console.WriteLine(errortext.Value.Trim());
        }
        else
        {
            Console.WriteLine("No error for file.");
        }
    }
    Console.Write("Press Enter to quit");
    Console.ReadLine();
}