Click here to Skip to main content
Click here to Skip to main content

Wildcard string compare (globbing)

By , 15 Feb 2005
 

Usage:

This is a fast, lightweight, and simple pattern matching function.

if (wildcmp("bl?h.*", "blah.jpg")) {
  //we have a match!
} else {
  //no match =(
}

Function:

int wildcmp(const char *wild, const char *string) {
  // Written by Jack Handy - <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A>
  const char *cp = NULL, *mp = NULL;

  while ((*string) && (*wild != '*')) {
    if ((*wild != *string) && (*wild != '?')) {
      return 0;
    }
    wild++;
    string++;
  }

  while (*string) {
    if (*wild == '*') {
      if (!*++wild) {
        return 1;
      }
      mp = wild;
      cp = string+1;
    } else if ((*wild == *string) || (*wild == '?')) {
      wild++;
      string++;
    } else {
      wild = mp;
      string = cp++;
    }
  }

  while (*wild == '*') {
    wild++;
  }
  return !*wild;
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Jack Handy
Web Developer
United States United States
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralDoesnt seem to work well..sussBikram Singh13 May '04 - 1:56 
Tried these wildcards, and they show different results in your code and in Windows Explorer's search command.
 
??x*
*so*
??so*
??so??
 
Lack of comments in the code also make it a bit difficult to understand. On the whole however, good job!

 

Bikram

GeneralRe: Doesnt seem to work well..memberJack Handy21 Jun '04 - 9:13 
The Windows Explorer search is not a straight wildcard match. It essentially adds *'s to either end of your input string so "b?r" matches "foobar.txt". I take a more literal approach. This function does not presume to be smarter than the caller. It is not only meant for files, it is also very useful for checking hostmasks for example.
 
If you think my function is producing bad results, can you paste an example of the string along with the wildcard?
 
Thanks,
 
Jack
 

There are 10 types of people in this world, those that understand binary and those who don't.


GeneralCase Insensitive wildcmpmemberTechiex16 Mar '04 - 9:36 
I want case insenstive wildcmp function. Could anyone help me?
GeneralRe: Case Insensitive wildcmpmemberNeville Franks16 Mar '04 - 9:52 
Simply wrap the code that compares charaters in toupper() calls.
 
eg.
    if ((toupper(*wild) != toupper(*string)) && (*wild != '?')) {

 
Neville Franks, Author of ED for Windows www.getsoft.com and coming soon: Surfulater www.surfulater.com
 

GeneralRe: Case Insensitive wildcmpmemberTechiex23 Mar '04 - 9:33 
toupper doesnt support char characters. You could optimize this code.
This code compares also multilingual characters
 


#include

#define BIT5 0x20

char buf[] = "this is ®Ñê test";
char *pbuf;

int lower(int ch);


int lower(int ch)
{
if ((ch==64)||(ch==91)||(ch==92)||(ch==93))
return ch &= ~(ch & BIT5);
return ch |= BIT5;
}

int wildcmp(char *wild, char *string) {
char *cp, *mp;

while ((*string) && (*wild != '*')) {
if ((*wild != *string) && (*wild != '?')) {
return 0;
}
wild++;
string++;
}

while (*string) {
if (*wild == '*') {
if (!*++wild) {
return 1;
}
mp = wild;
cp = string+1;
} else if ((*wild == *string) || (*wild == '?')) {
wild++;
string++;
} else {
wild = mp;
string = cp++;
}
}
 
while (*wild == '*') {
wild++;
}
return !*wild;
}
 

int main() {

for (pbuf = &buf[0]; *pbuf; ++pbuf)
*pbuf= lower (*pbuf);

if (wildcmp("*®ñê*", buf)){
printf ("match : %s \n", buf);
} else {
printf ("not match: %s \n",buf);
}
}



GeneralRe: Case Insensitive wildcmpmemberDavidCrow23 Feb '05 - 2:24 
Techiex wrote:
toupper doesnt support char characters
 
Since when?
 
ASSERT('A' == toupper('a'));

 

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow


GeneralRe: Case Insensitive wildcmpmemberVic Mackey23 Feb '05 - 8:00 
Be careful when using toupper(), some of the CRT variations of this function _only_ work when the input is known to be lowercase. For example, the return value is invalid for _toupper('A') and some implementations of toupper('A') as well...check the documentation.

GeneralRe: Case Insensitive wildcmpmemberDavidCrow23 Feb '05 - 8:22 
Vic Mackey wrote:
Be careful when using toupper(), some of the CRT variations of this function _only_ work when the input is known to be lowercase.
 
Which is why I specified toupper() instead of _toupper(). The latter is nothing but a #define directive that does no checking.
 
In any case, I was simply responding to Techiex's statement that "toupper doesnt support char characters."
 

"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow


GeneralRe: Case Insensitive wildcmpmemberf_randy12 Jun '06 - 18:13 
Doesnt seems to work for me.
GeneralExcellent code!memberHans Dietrich16 Jul '03 - 19:40 
Very nice, compact, works great and is very fast. Thanks, Jack!
 
Best wishes,
Hans

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130523.1 | Last Updated 15 Feb 2005
Article Copyright 2001 by Jack Handy
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid