正则表达式 – 如何检查文件名是否匹配通配符模式
发布时间: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; } } } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ios – 在Swift中获取图像名称UIImagePickerController
- ruby-on-rails – Docker – PG :: ConnectionBad
- ruby-on-rails – 如何为特色对象预填充_form?
- AJAX跨域请求 功能实现
- 闪光 – 在Actionscript 3中拖动时检测鼠标离开舞台
- 让我为之兴奋的VB.NET体验之旅
- postgresql – Postgres 8.3:“错误:缓存计划不能更改结果
- c – 如果一个过期的weak_ptr会给出未定义的行为,那么owner
- 自定义attrs.xml与styles.xml还有Theme(主题)
- SQLite3 for WinCE or Mobile (EVC篇)