【发布时间】:2019-12-26 07:19:16
【问题描述】:
我正在尝试使用正则表达式来匹配文件类型的部分名称,然后计算匹配的结果数量并显示这些结果。 例如,如果正则表达式正在查找其中包含“germany”字样的文件名,并且其中有 5 个包含“germany”的文件,我希望能够将这些匹配项从总数中计算出来并说“你有 5 个德国提到”
我设置了要计数的目录的路径,并设法将匹配的正则表达式与至少一种类型匹配(我想为多个表达式执行此操作。)并使用文件扩展名使用 foreach 对它们进行计数。
public static void russianBias()
{
int x = 0;
string[] replayslol = System.IO.Directory.GetFiles(@"C:\Games\World_of_Tanks\replays", "*.wotreplay");
foreach (string file in replayslol)
{
string replayresult = string.Concat(replayslol);
Regex russia = new Regex(@"_ussr-");
foreach (Match match in russia.Matches(replayresult))
{
x++;
}
}
Console.WriteLine("You've played Russian vehicles {0}" + x + " times!");
//repeat for all nations
Console.WriteLine("Press any key to close.");
Console.ReadLine();```
The goal is to have this say a number of times for every nation in this game.
The issue is that the regex does not seem to quite match the filenames and actually matches too many items.
【问题讨论】:
标签: c# regex filesystems counting