System.StringComparer that supports wildcard (*)

2019-02-19 05:21发布

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

3条回答
Lonely孤独者°
2楼-- · 2019-02-19 05:31

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
}
查看更多
一夜七次
3楼-- · 2019-02-19 05:34

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.

查看更多
迷人小祖宗
4楼-- · 2019-02-19 05:43

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("\\?", ".") + "$";
        }
    }
查看更多
登录 后发表回答