user-0 提问: 求助带通配符的字符串匹配问题

回复:
user: user-1
content: #include <stdio.h> #include <stdlib.h> #include <ctype.h>  //摘自《代码之美》 //match2(regexp,text) // 字符     含义 // .        匹配任意的单个字符 // ^        匹配输入字符串的开头 // $        匹配输入字符串的结尾 // *        匹配前一个字符的零个或者多个出现 int matchhere(char *regexp, char *text); int matchstar(int c, char *regexp, char *text) {// matchstar: search for c*regexp at beginning of text    do {// a * matches zero or more instances        if (matchhere(regexp, text)) return 1;    } while (*text != '\0' && (*text++ == c || c == '.'));    return 0; } int matchhere(char *regexp, char *text) {// matchhere: search for regexp at beginning of text    if (regexp[0] == '\0') return 1;    if (regexp[1] == '*') return matchstar(regexp[0], regexp+2, text);    if (regexp[0] == '$' && regexp[1] == '\0') return *text == '\0';    if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text)) return matchhere(regexp+1, text+1);    return 0; } int match2(char *regexp, char *text) {// match: search for regexp anywhere in text     if (regexp[0] == '^') return matchhere(regexp+1, text);     do {// must look even if string is empty         if (matchhere(regexp, text)) return 1;     } while (*text++ != '\0');     return 0; }  //match1(regexp,text) // 字符     含义 // ?        匹配任意的单个字符 // *        匹配零个或者多个字符 int match_imp(const char *d,int dcur,const char *s,int scur) {     if(!d[dcur]) return (!s[scur])?1:0;     if (d[dcur]=='?') return match_imp(d,dcur+1,s,scur+1);     else if(d[dcur]=='*') {         do {             if (match_imp(d,dcur+1,s,scur)) return 1;         } while (s[scur++]);         return 0;     } else return (tolower(d[dcur])==tolower(s[scur]) && match_imp(d,dcur+1,s,scur+1))?1:0; } int match1(char* s1, char* s2) {     return match_imp(s1,0,s2,0); }  int main() {     printf("%d==match1(abc ,abc)\n",match1("abc" ,"abc"));     printf("%d==match1(a?c ,abc)\n",match1("a?c" ,"abc"));     printf("%d==match1(a*c ,abc)\n",match1("a*c" ,"abc"));     printf("-------------------\n");     printf("%d==match1(abc ,abd)\n",match1("abc" ,"abd"));     printf("%d==match1(a?c ,abd)\n",match1("a?c" ,"abd"));     printf("%d==match1(a*c ,abd)\n",match1("a*c" ,"abd"));     printf("\n");     printf("%d==match2(abc ,abc)\n",match2("abc" ,"abc"));     printf("%d==match2(^a  ,abc)\n",match2("^a"  ,"abc"));     printf("%d==match2(c$  ,abc)\n",match2("c$"  ,"abc"));     printf("%d==match2(a.c ,abc)\n",match2("a.c" ,"abc"));     printf("%d==match2(a.*c,abc)\n",match2("a.*c","abc"));     printf("-------------------\n");     printf("%d==match2(ABC ,abc)\n",match2("ABC" ,"abc"));     printf("%d==match2(^B  ,abc)\n",match2("^B"  ,"abc"));     printf("%d==match2(A$  ,abc)\n",match2("A$"  ,"abc"));     printf("%d==match2(a..c,abc)\n",match2("a..c","abc"));     printf("%d==match2(a.*d,abc)\n",match2("a.*d","abc"));     return 0; } //1==match1(abc ,abc) //1==match1(a?c ,abc) //1==match1(a*c ,abc) //------------------- //0==match1(abc ,abd) //0==match1(a?c ,abd) //0==match1(a*c ,abd) // //1==match2(abc ,abc) //1==match2(^a  ,abc) //1==match2(c$  ,abc) //1==match2(a.c ,abc) //1==match2(a.*c,abc) //------------------- //0==match2(ABC ,abc) //0==match2(^B  ,abc) //0==match2(A$  ,abc) //0==match2(a..c,abc) //0==match2(a.*d,abc) //

user: user-2
content: 应该是简单的递归 bool match_imp(const char *s,int scur,const char *d,int dcur) {     if(!d[dcur]) return !s[scur];     if(d[dcur]=='?') return match_imp(s,scur+1,d,dcur+1);     else if(d[dcur]=='*')     {         do{             if(match_imp(s,scur,d,dcur+1)) return true;         }while(s[scur++]);         return false;     }else return tolower(s[scur])==tolower(d[dcur])&&match_imp(s,scur+1,d,dcur+1); } int match(char* s1, char* s2) {     return match_imp(s1,0,s2,0); }

user: user-3
content: MARK一个。。。。。

user: user-1
content: 2楼是说这个代码不支持多个* ？ 你试运行过了？

user: user-4
content: 引用 1 楼 zhao4zhong1 的回复:仅供参考：//摘自《代码之美》 // 字符     含义 // .        匹配任意的单个字符 // ^        匹配输入字符串的开头 // $        匹配输入字符串的结尾 // *        匹配前一个字符的零个或者多个出现 #include <stdio.h> int matchhere(char *regexp, char *text);  int matchstar(int c, char *regexp, char *text) {// matchstar: search for c*regexp at beginning of text    do {// a * matches zero or more instances        if (matchhere(regexp, text)) return 1;    } while (*text != '\0' && (*text++ == c || c == '.'));    return 0; } int matchhere(char *regexp, char *text) {// matchhere: search for regexp at beginning of text    if (regexp[0] == '\0') return 1;    if (regexp[1] == '*') return matchstar(regexp[0], regexp+2, text);    if (regexp[0] == '$' && regexp[1] == '\0') return *text == '\0';    if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text)) return matchhere(regexp+1, text+1);    return 0; }  int match(char *regexp, char *text) {// match: search for regexp anywhere in text     if (regexp[0] == '^') return matchhere(regexp+1, text);     do {// must look even if string is empty         if (matchhere(regexp, text)) return 1;     } while (*text++ != '\0');     return 0; } void main() {     printf("%d==match(abc ,abc)\n",match("abc" ,"abc"));     printf("%d==match(^a  ,abc)\n",match("^a"  ,"abc"));     printf("%d==match(c$  ,abc)\n",match("c$"  ,"abc"));     printf("%d==match(a.c ,abc)\n",match("a.c" ,"abc"));     printf("%d==match(a.*c,abc)\n",match("a.*c","abc"));     printf("-------------------\n");     printf("%d==match(ABC ,abc)\n",match("ABC" ,"abc"));     printf("%d==match(^B  ,abc)\n",match("^B"  ,"abc"));     printf("%d==match(A$  ,abc)\n",match("A$"  ,"abc"));     printf("%d==match(a..c,abc)\n",match("a..c","abc"));     printf("%d==match(a.*d,abc)\n",match("a.*d","abc")); } //1==match(abc ,abc) //1==match(^a  ,abc) //1==match(c$  ,abc) //1==match(a.c ,abc) //1==match(a.*c,abc) //------------------- //0==match(ABC ,abc) //0==match(^B  ,abc) //0==match(A$  ,abc) //0==match(a..c,abc) //0==match(a.*d,abc)  这个例子相当漂亮，不过 多个通配符（主要是多个*号），必须另外处理，因为中间的字符串是可伸缩的 最好的办法是两头向中间匹配 单个字符的通配符，以及固定个数个字符，都比较简单，因为只有相等才算匹配， 如果，采用近似匹配（模糊匹配？），那就更麻烦了，就必须知道如何近似才行。

user: user-1
content: 仅供参考：//摘自《代码之美》 // 字符     含义 // .        匹配任意的单个字符 // ^        匹配输入字符串的开头 // $        匹配输入字符串的结尾 // *        匹配前一个字符的零个或者多个出现 #include <stdio.h> int matchhere(char *regexp, char *text);  int matchstar(int c, char *regexp, char *text) {// matchstar: search for c*regexp at beginning of text    do {// a * matches zero or more instances        if (matchhere(regexp, text)) return 1;    } while (*text != '\0' && (*text++ == c || c == '.'));    return 0; } int matchhere(char *regexp, char *text) {// matchhere: search for regexp at beginning of text    if (regexp[0] == '\0') return 1;    if (regexp[1] == '*') return matchstar(regexp[0], regexp+2, text);    if (regexp[0] == '$' && regexp[1] == '\0') return *text == '\0';    if (*text!='\0' && (regexp[0]=='.' || regexp[0]==*text)) return matchhere(regexp+1, text+1);    return 0; }  int match(char *regexp, char *text) {// match: search for regexp anywhere in text     if (regexp[0] == '^') return matchhere(regexp+1, text);     do {// must look even if string is empty         if (matchhere(regexp, text)) return 1;     } while (*text++ != '\0');     return 0; } void main() {     printf("%d==match(abc ,abc)\n",match("abc" ,"abc"));     printf("%d==match(^a  ,abc)\n",match("^a"  ,"abc"));     printf("%d==match(c$  ,abc)\n",match("c$"  ,"abc"));     printf("%d==match(a.c ,abc)\n",match("a.c" ,"abc"));     printf("%d==match(a.*c,abc)\n",match("a.*c","abc"));     printf("-------------------\n");     printf("%d==match(ABC ,abc)\n",match("ABC" ,"abc"));     printf("%d==match(^B  ,abc)\n",match("^B"  ,"abc"));     printf("%d==match(A$  ,abc)\n",match("A$"  ,"abc"));     printf("%d==match(a..c,abc)\n",match("a..c","abc"));     printf("%d==match(a.*d,abc)\n",match("a.*d","abc")); } //1==match(abc ,abc) //1==match(^a  ,abc) //1==match(c$  ,abc) //1==match(a.c ,abc) //1==match(a.*c,abc) //------------------- //0==match(ABC ,abc) //0==match(^B  ,abc) //0==match(A$  ,abc) //0==match(a..c,abc) //0==match(a.*d,abc)