我有一个通配符模式,可能是"*.txt"或"POS ??.dat".
我还有内存中的文件名列表,我需要与该模式进行比较.
我将如何做到这一点,请记住,我需要与IO.DirectoryInfo.GetFiles(模式)使用完全相同的语义.
编辑:盲目地将其转换为正则表达式将无法正常工作.
我在代码中有一个完整的答案,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) { Listmatches = new List (); 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; } } } }
你可以这么做.您不需要正则表达式.
using Microsoft.VisualBasic.CompilerServices; if (Operators.LikeString("pos123.txt", "pos?23.*", CompareMethod.Text)) { Console.WriteLine("Filename matches pattern"); }
或者,在VB.Net中,
If "pos123.txt" Like "pos?23.*" Then
Console.WriteLine("Filename matches pattern")
End If
在c#中,您可以使用扩展方法对其进行模拟.它不会像VB Like那样,但它会......非常酷.