wildcard matching

转载:http://www.cnblogs.com/codingmylife/archive/2012/10/05/2712585.html

‘?’匹配任意单个字符,‘*’匹配任意字符序列(包括空字符序列)。如果匹配整个串返回true。

例:

isMatch("aa","a") → false
isMatch("aa","aa") → true
isMatch("aaa","aa") → false
isMatch("aa", "*") → true
isMatch("aa", "a*") → true
isMatch("ab", "?*") → true
isMatch("aab", "c*a*b") → false
// Recursion version.
bool isMatch(const char *s, const char *p) 
{
    if (s == NULL || p == NULL) return false;
    if (*p == '\0') return *s == '\0';
    
    if (*p == '*')
    {
        while (*p == '*') ++p;
        
        while (*s != '\0')
        {
            if (isMatch(s, p)) return true;
            ++s;
        }
        
        return isMatch(s, p);
    }
    else if ((*s != '\0' && *p == '?') || *p == *s)
    {
        return isMatch(s + 1, p + 1);
    }
    
    return false;
}

点赞