标签:style blog color io for ar div amp
?匹配任意一个字符,*匹配任务多个字符(包括0)
#include <stdio.h> #include <string.h> int match(const char *src, const char *pattern) { if (src == NULL || pattern == NULL) { return 0; } const char *tmp_pat = pattern; while (*tmp_pat && *tmp_pat == ‘*‘) { tmp_pat++; } int srcLen = strlen(src); int patternLen = strlen(tmp_pat); int starcount = 0; for (int i = 0; i < patternLen; i++) { if (tmp_pat[i] == ‘*‘) { starcount++; } } if (patternLen - starcount > srcLen) { return 0; } int i = 0; int j = 0; while (i <= srcLen - (patternLen - starcount) && j < patternLen - starcount && tmp_pat[j] != ‘*‘) { if (tmp_pat[j] == ‘?‘ || src[i + j] == tmp_pat[j]) { j++; } else { j = 0; i++; } } if (j == patternLen - starcount) { return 1; } if (tmp_pat[j] == ‘*‘) { i += j; return match(src + i, tmp_pat + j); } return 0; } int main(int argc, char **argv) { char *src = "wo shi yi ge zhong guo ren"; src = "shi"; char *pat = "shi"; if (match(src, pat)) { printf("match\n"); } pat = "*sh*?*"; if (match(src, pat)) { printf("match\n"); } getchar(); return 0; }
标签:style blog color io for ar div amp
原文地址:http://www.cnblogs.com/liuzhijiang123/p/3877341.html