[comp.lang.c] Wanted: string matching routine

vahid@vesta.ics.uci.edu (Frank Vahid) (06/27/91)

Hi.

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.

Thanks.

Frank (vahid@ics.uci.edu)

ok@goanna.cs.rmit.oz.au (Richard A. O'Keefe) (06/27/91)

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.

If you want UNIX-style wildcards, you may be using a UNIX system.
In that case, try 'man regex' or 'man regexp'.
Or look at Kernighan & Plauger "Software Tools" and turn their stuff
back from Ratfor to C.

-- 
I agree with Jim Giles about many of the deficiencies of present UNIX.

kremer@cs.odu.edu (Lloyd Kremer) (06/27/91)

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.

So would strncmp("abc", "abcdefg", 3).

While there is no wildcard capability in the str* routines, it is fairly
easy to combine them to produce the desired result.

Some almost have a 'grep' type of action like finding a substring within
a string.

					Lloyd Kremer
					Hilton Systems, Inc.
					kremer@cs.odu.edu

mouse@thunder.mcrcim.mcgill.edu (der Mouse) (06/29/91)

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