Vergleich C-String mit Wildcards



  • Gibt es eine elegante Möglichkeiten 2 CStrings miteinander zu vergleichen, wenn einer Wildcars enthält (zB.: "123456789AB" mit "123*789?B")! Oder muss ich mir das mit einigen Schleifen und einsetzen der verschiedenen Möglichkeiten selber schreiben?

    Danke


  • Administrator

    such mal in der MSDN nach strcmp und stricmp ... ich glaube das ist es was du suchst.
    Grüssli



  • Wildcard string compare (globbing)
    http://www.codeproject.com/string/wildcmp.asp


  • Mod

    Dieser Code funktioniert nichtkorrekt. Er arbeitet vor allem bei bestimmten nicht korrekt.
    wild = "*?.abc", str = "abc.abc".

    Ohne Rekursion ist das nicht zu machen...

    bool Match(PCTSTR pszText, PCTSTR pszMatch, bool bMatchCase=false)
    {
        // Loop over the string
        while (*pszText && *pszMatch!=_T('*')) 
        {
            // Found a match on a next normal character
            if ((bMatchCase ? 
                    *pszText!=*pszMatch : 
                    CharUpper(reinterpret_cast<PTSTR>(MAKELONG(*pszText,0)))!=CharUpper(reinterpret_cast<PTSTR>(MAKELONG(*pszMatch,0)))) &&
                *pszMatch!=_T('?')) 
                return false;
    
            // still a match
            pszMatch++;
            pszText++;
        }
    
        // Either we have a wildcard or or we are at end of the string to test
        if (!*pszText)
        {
            // There is a special case were there is only a wildcard char left
            // on the match string, so we just skip it
            while (*pszMatch==_T('*'))
                ++pszMatch;
            // it would be a match if both strings reached the end of the string
            return !*pszText && !*pszMatch;
        }
        else 
        {
            // We found a wildcard '*'. We have some chances now:
            // 1. we just ignore the wildcard and continue
            // 2. we just skip 1 character in the source and reuse the wildcard
            if (Match(pszText,pszMatch+1,bMatchCase))
                return true;
            else
                return Match(pszText+1,pszMatch,bMatchCase);
        }
    }
    

Anmelden zum Antworten