0

need to identify "errors" in text files and show them in console need help with the regex and extracting only the error out of the text as now it shows the full text.

text i need to identify is in brackets Pay us 2 Bitcoins now to: { 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 ",);
    }
}   
stuartd
  • 70,509
  • 14
  • 132
  • 163

1 Answers1

0

I used the Regex Here from Milosz.

Your code, slightly modified:

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();
}
Idle_Mind
  • 38,363
  • 3
  • 29
  • 40