返回: 1
参考了《代码之美》第一章---正则表达式匹配器 :http://blog.chinaunix.net/uid-26822401-id-3150018.html
#include <iostream> #include <cstring> using namespace std; int GetMatchSta (const char *ArrStr,const char *KeyStr); int matchhere(const char *arrstr,const char *keystr); int matchstar(const char *arrstr,char ch,const char *keystr);//处理 * 的情况 int main() { char *arr="tommababcabc"; char *key="t?m*ab*abc"; cout<<GetMatchSta(arr,key)<<endl; } int GetMatchSta (const char *ArrStr,const char *KeyStr) { if(KeyStr[0]=='*' && KeyStr[1]!='\0' && KeyStr[1]!='?') return matchstar(ArrStr,KeyStr[1],KeyStr+1); if(KeyStr[0]=='*' && (KeyStr[1]=='?'))// "*?" 这种情况匹配所有除非源串为空 return strlen(ArrStr)!=0; if(KeyStr[0]=='*' && KeyStr[1]=='\0') // "*" 这情况匹配所有,包括空串 return 1; if(KeyStr[0]=='?') return matchhere(ArrStr+1,KeyStr+1); return matchhere(ArrStr,KeyStr); } int matchhere(const char *arrstr,const char *keystr) { if(keystr[0]=='\0') return *arrstr=='\0'; if(keystr[0]=='*') return matchstar(arrstr,keystr[1],keystr+1); if(*arrstr!='\0' && (keystr[0]==arrstr[0] || keystr[0]=='?')) return matchhere(arrstr+1,keystr+1); return 0; } int matchstar(const char *arrstr,char ch,const char *keystr) { if(ch=='\0') return 1; //以 * 结尾 do{ if(matchhere(arrstr,keystr)) return 1; } while( *arrstr++ !='\0'); return 0; }