需要识别文本文件中的“错误”并将其显示在控制台中 正则表达式需要帮助,并且可以从文本中仅提取错误,因为它现在显示了全文。
我需要识别的文本放在方括号中 现在付给我们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 ",);
}
}
答案 0 :(得分:0)
您的代码,略有修改:
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();
}