System.StringComparer that supports wildcard (*)

后端 未结 3 342
梦如初夏
梦如初夏 2021-01-14 11:17

I\'m looking for a fast .NET class/library that has a StringComparer that supports wildcard (*) AND incase-sensitivity. Any Ideas?

相关标签:
3条回答
  • 2021-01-14 11:39

    alternatively you can try following

    class Wildcard : Regex
        {
            public Wildcard() { }
            public Wildcard(string pattern) : base(WildcardToRegex(pattern)) { }
            public Wildcard(string pattern, RegexOptions options) : base(WildcardToRegex(pattern), options) { }
            public static string WildcardToRegex(string pattern)
            {
                return "^" + Regex.Escape(pattern).
                Replace("\\*", ".*").
                Replace("\\?", ".") + "$";
            }
        }
    
    0 讨论(0)
  • 2021-01-14 11:42

    You could use Regex with RegexOptions.IgnoreCase, then compare with the IsMatch method.

    var wordRegex = new Regex( "^" + prefix + ".*" + suffix + "$", RegexOptions.IgnoreCase );
    
    if (wordRegex.IsMatch( testWord ))
    {
        ...
    }
    

    This would match prefix*suffix. You might also consider using StartsWith or EndsWith as alternatives.

    0 讨论(0)
  • 2021-01-14 11:50

    Alternatively you can use these extended functions:

    public static bool CompareWildcards(this string WildString, string Mask, bool IgnoreCase)
    {
        int i = 0;
    
        if (String.IsNullOrEmpty(Mask))
            return false;
        if (Mask == "*")
            return true;
    
        while (i != Mask.Length)
        {
            if (CompareWildcard(WildString, Mask.Substring(i), IgnoreCase))
                return true;
    
            while (i != Mask.Length && Mask[i] != ';')
                i += 1;
    
            if (i != Mask.Length && Mask[i] == ';')
            {
                i += 1;
    
                while (i != Mask.Length && Mask[i] == ' ')
                    i += 1;
            }
        }
    
        return false;
    }
    
    public static bool CompareWildcard(this string WildString, string Mask, bool IgnoreCase)
    {
        int i = 0, k = 0;
    
        while (k != WildString.Length)
        {
            if (i > Mask.Length - 1)
                return false;
    
            switch (Mask[i])
            {
                case '*':
    
                    if ((i + 1) == Mask.Length)
                        return true;
    
                    while (k != WildString.Length)
                    {
                        if (CompareWildcard(WildString.Substring(k + 1), Mask.Substring(i + 1), IgnoreCase))
                            return true;
    
                        k += 1;
                    }
    
                    return false;
    
                case '?':
    
                    break;
    
                default:
    
                    if (IgnoreCase == false && WildString[k] != Mask[i])
                        return false;
                    if (IgnoreCase && Char.ToLower(WildString[k]) != Char.ToLower(Mask[i]))
                        return false;
    
                    break;
            }
    
            i += 1;
            k += 1;
        }
    
        if (k == WildString.Length)
        {
            if (i == Mask.Length || Mask[i] == ';' || Mask[i] == '*')
                return true;
        }
    
        return false;
    }
    

    CompareWildcards compares a string against multiple wildcard patterns, and CompareWildcard compares a string against a single wildcard pattern.

    Example usage:

    if (Path.CompareWildcards("*txt;*.zip;", true) == true)
    {
        // Path matches wildcard
    }
    
    0 讨论(0)
提交回复
热议问题