Click here to Skip to main content
Licence 
First Posted 1 May 2001
Views 606,915
Bookmarked 85 times

Wildcard string compare (globbing)

By Jack Handy | 15 Feb 2005
Matches a string against a wildcard string such as "*.*" or "bl?h.*" etc. This is good for file globbing or to match hostmasks.
4 votes, 5.7%
1

2
1 vote, 1.4%
3
6 votes, 8.6%
4
59 votes, 84.3%
5
4.88/5 - 98 votes
4 removed
μ 4.61, σa 1.72 [?]

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


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. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 5 PinmemberPlamen Petrov22:37 13 Dec '11  
SuggestionModification with '#' as wildcard joker for digits [modified] PinmemberThomas Haase0:16 26 Sep '11  
QuestionLicence Question Pinmemberrandommark1:33 23 Nov '10  
AnswerAnother C# version, with a twist Pinmembertomlev15:50 29 Jun '10  
GeneralObscurity PinmemberChuck O'Toole19:18 25 Apr '10  
AnswerMy C# contribution - recursive, of course! PinmemberRenniePet6:21 26 Mar '10  
GeneralRe: My C# contribution - recursive, of course! PinmemberErwin de GRoot2:58 29 Mar '10  
GeneralDepends on whether you need to optimize the last few nanoseconds out of it... PinmemberRenniePet8:45 29 Mar '10  
GeneralSorry - revised numbers PinmemberRenniePet9:35 29 Mar '10  
GeneralRe: Depends on whether you need to optimize the last few nanoseconds out of it... PinmemberErwin de GRoot9:37 29 Mar '10  
GeneralYet another version - 25% faster, I think [modified] PinmemberRenniePet9:24 1 Apr '10  
GeneralRe: Yet another version - 25% faster, I think Pinmemberaleks1k3:47 21 Sep '11  
QuestionI used this function but I how I can catch variables from the * ??? Pinmembermoh.hijjawi2:55 20 Oct '09  
AnswerRe: I used this function but I how I can catch variables from the * ??? PinmemberRenniePet12:27 1 Apr '10  
Questionany updates ? Pinmemberalhambra-eidos6:12 2 Jul '09  
GeneralImproved matching with end-of-text PinmemberAnders Heie16:20 11 May '09  
GeneralRe: Improved matching with end-of-text: some cases don't work properly! Pinmemberroadrunner3144:35 12 Aug '09  
QuestionPathMatchSpec instead? Pinmemberkintz9:55 25 Mar '09  
AnswerRe: PathMatchSpec instead? PinmemberMandatoryDefault11:39 31 Aug '09  
Questionwchar_t version? Pinmemberrmorales8721:16 29 Nov '08  
AnswerRe: wchar_t version? Pinmemberrazvar22:49 31 Mar '11  
Generalwildcmp in XBLite PinmemberCodeGibbon14:56 27 Nov '08  
GeneralWildcard string compare in C# Pinmemberhaiquang23:15 10 Nov '08  
GeneralRe: Wildcard string compare in C# Pinmemberhaiquang23:22 3 Aug '09  
GeneralC# Direct Port Pinmemberhempels16:10 23 Sep '08  

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

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

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