【问题标题】:How to count results of file names from a matched regular expression in C#如何从 C# 中匹配的正则表达式计算文件名的结果
【发布时间】: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


    【解决方案1】:

    我猜也许一个检查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);
            }
        }
    }
    

    如果您想探索/简化/修改表达式,它已经 在右上角的面板上进行了解释 regex101.com。如果你愿意,你 也可以在this link看,怎么搭配 针对一些样本输入。


    【讨论】:

      【解决方案2】:

      Directory.GetFiles 将返回 String[] 当您在该数组上使用 String.Concat 时,您将从数组中的所有字符串中创建一个字符串。

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

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

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

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

      例如:

      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

      【讨论】:

      • 效果很好,谢谢。不知道文件。包含是一件事!有用的知道大声笑
      猜你喜欢
      • 2011-11-14
      • 1970-01-01
      • 2013-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多