bool IsMatch(CString s, CString p) { int j = 0;//定义在外面,循环体外要用 for (int i = 0, last_p = 0, s_start = 0; i < s.GetLength();) { if (j < p.GetLength() && (s[i] == p[j] || p[j] == ‘?‘))// { i++; j++; } else if (j < p.GetLength() && p[j] == ‘*‘) { s_start = i; last_p = ++j; } else if (last_p != 0) { i = s_start++; j = last_p; } else { return false; } } for (; j < p.GetLength() && p[j] == ‘*‘; ++j); return j == p.GetLength(); }
注:
*:匹配任意多个
?:匹配任意一个
形参
s:要搜索的字符串,例如:“a1b2c3”
p:匹配规则,例如:“a?b*”