加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 如何检查文件名是否匹配通配符模式

发布时间:2020-12-14 06:38:17 所属栏目:百科 来源:网络整理
导读:我有一个通配符模式,也许是“* .txt”或“POS ??。dat”。 我还有内存中需要与该模式进行比较的文件名列表。 我应该如何做,请记住,我需要与IO.DirectoryInfo.GetFiles(pattern)使用完全相同的语义。 编辑:盲目地将其转换为正则表达式将无法正常工作。 我
我有一个通配符模式,也许是“* .txt”或“POS ??。dat”。

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

我应该如何做,请记住,我需要与IO.DirectoryInfo.GetFiles(pattern)使用完全相同的语义。

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

我有一个完整的答案在你的代码为95%像FindFiles(字符串)。

在该功能的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;
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读