在目录c#中查找具有匹配模式的文件?

mr_*_*air 7 .net c# regex io

 string fileName = "";

            string sourcePath = @"C:\vish";
            string targetPath = @"C:\SR";

            string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
            string destFile = System.IO.Path.Combine(targetPath, fileName);

            string pattern = @"23456780";
            var matches = Directory.GetFiles(@"c:\vish")
                .Where(path => Regex.Match(path, pattern).Success);

            foreach (string file in matches)
            {
                Console.WriteLine(file); 
                fileName = System.IO.Path.GetFileName(file);
                Console.WriteLine(fileName);
                destFile = System.IO.Path.Combine(targetPath, fileName);
                System.IO.File.Copy(file, destFile, true);

            }
Run Code Online (Sandbox Code Playgroud)

我的上述程序适用于单一模式.

我正在使用上面的程序来查找具有匹配模式的目录中的文件,但在我的情况下我有多个模式,所以我需要将string pattern变量中的多个模式作为数组传递,但我不知道如何操作Regex.Match中的那些模式.

谁能帮我?

ale*_*oot 9

你可以在正则表达式中加一个OR:

string pattern = @"(23456780|otherpatt)";
Run Code Online (Sandbox Code Playgroud)