篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c_cpp c ++中的简单匹配字符串与正则表达式只支持“*”和“?”通配符相关的知识,希望对你有一定的参考价值。
// GLOG Implementation
// Implementation of fnmatch that does not need 0-termination
// of arguments and does not allocate any memory,
// but we only support "*" and "?" wildcards, not the "[...]" patterns.
// It's not a static function for the unittest.
GOOGLE_GLOG_DLL_DECL bool SafeFNMatch_(const char* pattern,
size_t patt_len,
const char* str,
size_t str_len) {
size_t p = 0;
size_t s = 0;
while (1) {
if (p == patt_len && s == str_len) return true;
if (p == patt_len) return false;
if (s == str_len) return p+1 == patt_len && pattern[p] == '*';
if (pattern[p] == str[s] || pattern[p] == '?') {
p += 1;
s += 1;
continue;
}
if (pattern[p] == '*') {
if (p+1 == patt_len) return true;
do {
if (SafeFNMatch_(pattern+(p+1), patt_len-(p+1), str+s, str_len-s)) {
return true;
}
s += 1;
} while (s != str_len);
return false;
}
return false;
}
}
// EasyLogging implementation
/// @brief Matches wildcards, '*' and '?' only supported.
static bool wildCardMatch(const char* str, const char* pattern) {
while (*pattern) {
switch (*pattern) {
case '?':
if (!*str)
return false;
++str;
++pattern;
break;
case '*':
if (wildCardMatch(str, pattern + 1))
return true;
if (*str && wildCardMatch(str + 1, pattern))
return true;
return false;
break;
default:
if (*str++ != *pattern++)
return false;
break;
}
}
return !*str && !*pattern;
}
以上是关于c_cpp c ++中的简单匹配字符串与正则表达式只支持“*”和“?”通配符的主要内容,如果未能解决你的问题,请参考以下文章