.NET中是否有内置机制来匹配正则表达式以外的模式?我想使用UNIX样式(glob)通配符匹配(*=任何数字的任何字符).
我想将它用于面向最终用户的控件.我担心允许所有RegEx功能会非常混乱.
min*_*.dk 63
我喜欢我的代码更加语义,所以我编写了这个扩展方法:
using System.Text.RegularExpressions;
namespace Whatever
{
    public static class StringExtensions
    {
        /// <summary>
        /// Compares the string against a given pattern.
        /// </summary>
        /// <param name="str">The string.</param>
        /// <param name="pattern">The pattern to match, where "*" means any sequence of characters, and "?" means any single character.</param>
        /// <returns><c>true</c> if the string matches the given pattern; otherwise <c>false</c>.</returns>
        public static bool Like(this string str, string pattern)
        {
            return new Regex(
                "^" + Regex.Escape(pattern).Replace(@"\*", ".*").Replace(@"\?", ".") + "$",
                RegexOptions.IgnoreCase | RegexOptions.Singleline
            ).IsMatch(str);
        }
    }
}
(更改命名空间和/或将扩展方法复制到您自己的字符串扩展类)
使用此扩展,您可以编写如下语句:
if (File.Name.Like("*.jpg"))
{
   ....
}
只是糖,使你的代码更清晰:-)
Jon*_*son 36
我找到了你的实际代码:
Regex.Escape( wildcardExpression ).Replace( @"\*", ".*" ).Replace( @"\?", "." );
cle*_*ris 25
只是为了完整.自2016年以来,dotnet core有一个名为Microsoft.Extensions.FileSystemGlobbing支持高级全球路径的新nuget包.(Nuget套餐)
一些例子可能是,搜索在Web开发场景中非常常见的通配符嵌套文件夹结构和文件.
wwwroot/app/**/*.module.jswwwroot/app/**/*.js这与.gitignore用于确定要从源代码管理中排除哪些文件的文件有些类似.
Dan*_*lli 10
列表方法的2和3参数变体喜欢GetFiles()并将EnumerateDirectories()搜索字符串作为支持文件名通配的第二个参数,使用*和?.
class GlobTestMain
{
    static void Main(string[] args)
    {
        string[] exes = Directory.GetFiles(Environment.CurrentDirectory, "*.exe");
        foreach (string file in exes)
        {
            Console.WriteLine(Path.GetFileName(file));
        }
    }
}
会屈服
GlobTest.exe
GlobTest.vshost.exe
文档声明存在一些具有匹配扩展的警告.它还指出8.3文件名是匹配的(可能在幕后自动生成),这可能导致给定某些模式的"重复"匹配.
支持此方法是GetFiles(),GetDirectories()和GetFileSystemEntries().该Enumerate变种也支持这一点.
如果你想避免正则表达式,这是一个基本的 glob 实现:
public static class Globber
{
    public static bool Glob(this string value, string pattern)
    {
        int pos = 0;
        while (pattern.Length != pos)
        {
            switch (pattern[pos])
            {
                case '?':
                    break;
                case '*':
                    for (int i = value.Length; i >= pos; i--)
                    {
                        if (Glob(value.Substring(i), pattern.Substring(pos + 1)))
                        {
                            return true;
                        }
                    }
                    return false;
                default:
                    if (value.Length == pos || char.ToUpper(pattern[pos]) != char.ToUpper(value[pos]))
                    {
                        return false;
                    }
                    break;
            }
            pos++;
        }
        return value.Length == pos;
    }
}
像这样使用它:
Assert.IsTrue("text.txt".Glob("*.txt"));
我为 .NETStandard 编写了一个通配库,其中包含测试和基准测试。我的目标是为 .NET 生成一个具有最小依赖性的库,该库不使用正则表达式,并且性能优于正则表达式。
你可以在这里找到它:
如果使用VB.Net,则可以使用Like语句,它具有类似Glob的语法.