主题:[原创]通配符与目标字符串进行匹配的函数
作为练习,小弟写了个类似于通配符与目标字符串进行匹配的函数:
如: a?b 和 acb 可以匹配
a*b 和 abcdb 可以匹配
大家帮小弟看看,还能不能优化了?
大家也来发发自己的代码。不同的思路也能让小弟学学。
[code=c]
int process(char * express, char * string){
char *exp=express,*str=string,*starpos=NULL,*strpos=NULL;
if( !exp || !str ) return 0;// FAIL
while(str){
if(*str=='\0'){
return (*exp=='\0') || ( (*exp=='*') && (*(exp+1)=='\0') );
}
if(*exp=='\0'){
if(!starpos || (*starpos=='\0') )return 0;
exp=starpos;
}
switch(*exp){
case'?':
exp++,str++;
break;
case'*':
starpos=++exp,strpos=str;
if(*starpos=='\0')return 1;
break;
default:
if(*str==*exp){
exp++,str++;
}
else{
if(starpos && strpos && (*starpos=='\0') && (*strpos=='\0') )return 0;
exp=starpos,str=strpos++;
}
}
}
return 0;
}
[/code]
如: a?b 和 acb 可以匹配
a*b 和 abcdb 可以匹配
大家帮小弟看看,还能不能优化了?
大家也来发发自己的代码。不同的思路也能让小弟学学。
[code=c]
int process(char * express, char * string){
char *exp=express,*str=string,*starpos=NULL,*strpos=NULL;
if( !exp || !str ) return 0;// FAIL
while(str){
if(*str=='\0'){
return (*exp=='\0') || ( (*exp=='*') && (*(exp+1)=='\0') );
}
if(*exp=='\0'){
if(!starpos || (*starpos=='\0') )return 0;
exp=starpos;
}
switch(*exp){
case'?':
exp++,str++;
break;
case'*':
starpos=++exp,strpos=str;
if(*starpos=='\0')return 1;
break;
default:
if(*str==*exp){
exp++,str++;
}
else{
if(starpos && strpos && (*starpos=='\0') && (*strpos=='\0') )return 0;
exp=starpos,str=strpos++;
}
}
}
return 0;
}
[/code]