如何检查文件名是否与通配符模式匹配

Jon*_*len 28 .net regex

我有一个通配符模式,可能是"*.txt"或"POS ??.dat".

我还有内存中的文件名列表,我需要与该模式进行比较.

我将如何做到这一点,请记住,我需要与IO.DirectoryInfo.GetFiles(模式)使用完全相同的语义.

编辑:盲目地将其转换为正则表达式将无法正常工作.

spr*_*ite 45

我在代码中有一个完整的答案,95%喜欢FindFiles(string).

在此功能的MSDN文档的第二个注释中,不存在5%的短名称/长名称行为.

如果您仍想要获得该行为,则必须完成对输入数组中每个字符串的短名称的计算,然后将长名称添加到匹配集合中(如果长名称或短名称)匹配模式.

这是代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace FindFilesRegEx
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] names = { "hello.t", "HelLo.tx", "HeLLo.txt", "HeLLo.txtsjfhs", "HeLLo.tx.sdj", "hAlLo20984.txt" };
            string[] matches;
            matches = FindFilesEmulator("hello.tx", names);
            matches = FindFilesEmulator("H*o*.???", names);
            matches = FindFilesEmulator("hello.txt", names);
            matches = FindFilesEmulator("lskfjd30", names);
        }

        public string[] FindFilesEmulator(string pattern, string[] names)
        {
            List<string> matches = new List<string>();
            Regex regex = FindFilesPatternToRegex.Convert(pattern);
            foreach (string s in names)
            {
                if (regex.IsMatch(s))
                {
                    matches.Add(s);
                }
            }
            return matches.ToArray();
        }

        internal static class FindFilesPatternToRegex
        {
            private static Regex HasQuestionMarkRegEx   = new Regex(@"\?", RegexOptions.Compiled);
            private static Regex IllegalCharactersRegex  = new Regex("[" + @"\/:<>|" + "\"]", RegexOptions.Compiled);
            private static Regex CatchExtentionRegex    = new Regex(@"^\s*.+\.([^\.]+)\s*$", RegexOptions.Compiled);
            private static string NonDotCharacters      = @"[^.]*";
            public static Regex Convert(string pattern)
            {
                if (pattern == null)
                {
                    throw new ArgumentNullException();
                }
                pattern = pattern.Trim();
                if (pattern.Length == 0)
                {
                    throw new ArgumentException("Pattern is empty.");
                }
                if(IllegalCharactersRegex.IsMatch(pattern))
                {
                    throw new ArgumentException("Pattern contains illegal characters.");
                }
                bool hasExtension = CatchExtentionRegex.IsMatch(pattern);
                bool matchExact = false;
                if (HasQuestionMarkRegEx.IsMatch(pattern))
                {
                    matchExact = true;
                }
                else if(hasExtension)
                {
                    matchExact = CatchExtentionRegex.Match(pattern).Groups[1].Length != 3;
                }
                string regexString = Regex.Escape(pattern);
                regexString = "^" + Regex.Replace(regexString, @"\\\*", ".*");
                regexString = Regex.Replace(regexString, @"\\\?", ".");
                if(!matchExact && hasExtension)
                {
                    regexString += NonDotCharacters;
                }
                regexString += "$";
                Regex regex = new Regex(regexString, RegexOptions.Compiled | RegexOptions.IgnoreCase);
                return regex;
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


tod*_*dmo 11

你可以这么做.您不需要正则表达式.

using Microsoft.VisualBasic.CompilerServices;

if (Operators.LikeString("pos123.txt", "pos?23.*", CompareMethod.Text))
{
  Console.WriteLine("Filename matches pattern");
}
Run Code Online (Sandbox Code Playgroud)

或者,在VB.Net中,

If "pos123.txt" Like "pos?23.*" Then
  Console.WriteLine("Filename matches pattern")
End If
Run Code Online (Sandbox Code Playgroud)

在c#中,您可以使用扩展方法对其进行模拟.它不会像VB Like那样,但它会......非常酷.


小智 6

只需调用 Windows API 函数 PathMatchSpecExW() 即可。

[Flags]
public enum MatchPatternFlags : uint
{
    Normal          = 0x00000000,   // PMSF_NORMAL
    Multiple        = 0x00000001,   // PMSF_MULTIPLE
    DontStripSpaces = 0x00010000    // PMSF_DONT_STRIP_SPACES
}

class FileName
{
    [DllImport("Shlwapi.dll", SetLastError = false)]
    static extern int PathMatchSpecExW([MarshalAs(UnmanagedType.LPWStr)] string file,
                                       [MarshalAs(UnmanagedType.LPWStr)] string spec,
                                       MatchPatternFlags flags);

    /*******************************************************************************
    * Function:     MatchPattern
    *
    * Description:  Matches a file name against one or more file name patterns.
    *
    * Arguments:    file - File name to check
    *               spec - Name pattern(s) to search foe
    *               flags - Flags to modify search condition (MatchPatternFlags)
    *
    * Return value: Returns true if name matches the pattern.
    *******************************************************************************/

    public static bool MatchPattern(string file, string spec, MatchPatternFlags flags)
    {
        if (String.IsNullOrEmpty(file))
            return false;

        if (String.IsNullOrEmpty(spec))
            return true;

        int result = PathMatchSpecExW(file, spec, flags);

        return (result == 0);
    }
}
Run Code Online (Sandbox Code Playgroud)


Guf*_*ffa 5

您可以将通配符转换为正则表达式:

*.txt -> ^.+\.txt$

POS??.dat _> ^POS..\.dat$
Run Code Online (Sandbox Code Playgroud)

使用Regex.Escape方法将非通配符的字符转义为模式的文字字符串(例如转换".txt""\.txt")。

通配符*翻译为.+, 并?翻译为.

将 ^ 放在模式的开头以匹配字符串的开头,将 $ 放在结尾以匹配字符串的结尾。

现在您可以使用该Regex.IsMatch方法来检查文件名是否与模式匹配。