如何计算 C# 中匹配的正则表达式的文件名结果

How to count results of file names from a matched regular expression in C#

我正在尝试使用正则表达式来匹配文件类型的部分名称,然后计算匹配结果的数量并显示这些结果。 例如,如果正则表达式正在寻找其中包含单词 "germany" 的文件名,并且其中有 5 个包含 "germany" 的文件,我希望能够从总数中计算出这些匹配项并说"You have 5 germany mentions"

我已经设置了一个到目录的路径来计数,并且已经成功地获得了至少一种类型的匹配正则表达式(我想对多个表达式执行此操作。)并使用文件扩展名使用 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.

我猜想检查 Germany 的表达式可能会工作,带有 i 标志,然后您只需添加一个计数器,如果返回 true:

^(?=.*\bgermany\b).*$

测试

using System;
using System.Text.RegularExpressions;

public class Example
{
    public static void Main()
    {
        string pattern = @"^(?=.*\bgermany\b).*$";
        string input = @"some data germany some other data
some data Germany some other data
some data GERMANY some other data
some data France some other data
some data UK some other data";
        RegexOptions options = RegexOptions.Multiline | RegexOptions.IgnoreCase;

        foreach (Match m in Regex.Matches(input, pattern, options))
        {
            Console.WriteLine("'{0}' found at index {1}.", m.Value, m.Index);
        }
    }
}

If you wish to explore/simplify/modify the expression, it's been explained on the top right panel of regex101.com. If you'd like, you can also watch in this link, how it would match against some sample inputs.


Directory.GetFiles will return a String[] When you use String.Concat 在该数组上,您将从数组中的所有字符串中创建一个字符串。

当模式可以在字符串中出现多次时,以这种方式计算匹配项是不可靠的。请注意,您还使用了 2 次 foreach 并对所有连接的字符串执行计数。

我认为您可以只检查文件名是否仅与模式匹配,然后递增计数器。

在这种情况下,您指定为正则表达式 _ussr- 的模式也可以只是一个字符串。如果您只想检查文件名中是否出现字符串,则可以改用 String.Contains

如果您确实想使用正则表达式,可以将对 file.Contains 的调用替换为 Regex.IsMatch,这也会 return 一个布尔值。

例如:

int russiaCount = 0;
int germanyCount = 0;
string[] replayslol = System.IO.Directory.GetFiles(@"./replays", "*.wotreplay");
foreach (string file in replayslol)
{
    if (file.Contains("_ussr-")) {
        russiaCount++;
    }
    if (file.Contains("_germany-")) {
        germanyCount++;
    }
}
Console.WriteLine("You've played Russian vehicles {0} times and German vehicles {1} times!", russiaCount, germanyCount);

C# demo