Pergunta

Eu estou procurando uma classe rápido .NET / biblioteca que tem um StringComparer que suportes curinga (*) E meter-sensibilidade. Todas as idéias?

Nenhuma solução correta

Outras dicas

Você pode usar Regex com RegexOptions.IgnoreCase, e depois comparar com o método IsMatch.

var wordRegex = new Regex( "^" + prefix + ".*" + suffix + "$", RegexOptions.IgnoreCase );

if (wordRegex.IsMatch( testWord ))
{
    ...
}

Isto corresponde prefix*suffix. Você também pode considerar o uso StartsWith ou EndsWith como alternativas.

Como alternativa, você pode usar essas funções estendidas:

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 compara uma seqüência contra vários padrões de coringas, e CompareWildcard compara uma corda contra um único padrão de curinga.

Exemplo de utilização:

if (Path.CompareWildcards("*txt;*.zip;", true) == true)
{
    // Path matches wildcard
}

Como alternativa, você pode tentar seguir

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("\\?", ".") + "$";
        }
    }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top