Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / C++
Article

Wildcard string compare (globbing)

Rate me:
Please Sign up or sign in to vote.
4.90/5 (82 votes)
15 Feb 2005 1.2M   96   144
Matches a string against a wildcard string such as "*.*" or "bl?h.*" etc. This is good for file globbing or to match hostmasks.

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


Written By
Web Developer
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralImproved matching with end-of-text Pin
Anders Heie11-May-09 15:20
Anders Heie11-May-09 15:20 
GeneralRe: Improved matching with end-of-text: some cases don't work properly! Pin
roadrunner31412-Aug-09 3:35
roadrunner31412-Aug-09 3:35 
some cases don't work properly:

wildstring = "a*bc"
matchstring = "abbc"
should be true, but it returns false

wildstring = "a*b"
matchstring = "a"
should be false, but it returns true

wildstring = "a*?b"
matchstring = "axb"
should be true, but it returns false

wildstring = "a**b"
matchstring = "axb"
should be true, but it returns false (ok, the two ** aren't useful, but they should work)

I solved the last 3 bugs, but the first one is a bit tricky...
bool StrWildCmp(char* wildstring, char *matchstring){
   char stopstring[1];
   *stopstring = '\0';

   while(*matchstring != '\0')
   {
      if (*wildstring == '*') 
      {
         do
         {         
            wildstring++;            
         } while (*wildstring == '*');  // if a dork entered two or more * in a row 
                                        // ignore them and go ahead
         
         if (*wildstring == '\0')   // if * was the last char, the strings are equal
         {
            return TRUE;
         }
         else
         {
            *stopstring = *wildstring; // the next char to check after the *
         }
      }

      if(*stopstring != '\0')
      {
         if((*stopstring == *matchstring) || (*stopstring == '?') ) 
         {
            wildstring++;
            *stopstring = '\0';
         }
         matchstring++;
      }
      else
         if((*wildstring == *matchstring) || (*wildstring == '?'))
         {
            wildstring++;
            matchstring++;
         }
         else
         {
            return FALSE;
         }

      if( (*matchstring == '\0') && (*wildstring != '\0') )
      {
         // matchstring seems to be too short. Check if wildstring has any more chars except '*'
         while (*wildstring == '*') // ignore following '*'
            wildstring++;
         
         if (*wildstring == '\0') // if wildstring endet after '*', strings are equal
            return TRUE;
         else
            return FALSE;
      }
}

GeneralRe: Improved matching with end-of-text: some cases don't work properly! Pin
sdiazdiaz20-Jun-13 1:50
sdiazdiaz20-Jun-13 1:50 

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

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.