Path: utzoo!utgpu!news-server.csri.toronto.edu!bonnie.concordia.ca!thunder.mcrcim.mcgill.edu!mouse From: mouse@thunder.mcrcim.mcgill.edu (der Mouse) Newsgroups: comp.lang.c Subject: Re: Wanted: string matching routine Message-ID: <1991Jun29.124352.21739@thunder.mcrcim.mcgill.edu> Date: 29 Jun 91 12:43:52 GMT References: <2868E3EF.6133@ics.uci.edu> Organization: McGill Research Centre for Intelligent Machines Lines: 43 In article <2868E3EF.6133@ics.uci.edu>, vahid@vesta.ics.uci.edu (Frank Vahid) writes: > Does anyone have a routine which is similar to strcmp, but permits > unix-type wildcard characters in at least one of the strings? For > example, strmatch("abc*", "abcdefg") would return a value denoting a > successful match. I've written such things fairly often. If you don't particularly care about blinding speed, it's pretty easy to do something like this (warning: untested) strmatch(pat,str) char *pat; char *str; { while (1) { switch (*pat) { case '\0': return(*str == '\0'); break; case '?': if (*str == '\0') return(0); break; case '*': if (strmatch(pat+1,str)) return(1); if (*str) str ++; pat ++; continue; /* skip the increments below */ break; default: if (*str != *pat) return(0); break; } pat ++; str ++; } } der Mouse old: mcgill-vision!mouse new: mouse@larry.mcrcim.mcgill.edu