Click here to Skip to main content
6,595,854 members and growing! (17,645 online)
Email Password   helpLost your password?
General Programming » String handling » Regular Expressions     Intermediate

Wildcard string compare (globbing)

By Jack Handy

Matches a string against a wildcard string such as "*.*" or "bl?h.*" etc. This is good for file globbing or to match hostmasks.
VC6Win2K, Visual Studio, Dev
Posted:1 May 2001
Updated:15 Feb 2005
Views:339,289
Bookmarked:74 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
94 votes for this article.
Popularity: 9.06 Rating: 4.59 out of 5
4 votes, 6.1%
1

2
1 vote, 1.5%
3
6 votes, 9.1%
4
55 votes, 83.3%
5

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 - jakkhandy@hotmail.com

  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


Member

Occupation: Web Developer
Location: United States United States

Other popular String handling articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 25 of 120 (Total in Forum: 120) (Refresh)FirstPrevNext
GeneralI used this function but I how I can catch variables from the * ??? Pinmembermoh.hijjawi2:55 20 Oct '09  
Generalany 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  
GeneralPathMatchSpec instead? Pinmemberkintz9:55 25 Mar '09  
GeneralRe: PathMatchSpec instead? PinmemberMandatoryDefault11:39 31 Aug '09  
Generalwchar_t version? Pinmemberrmorales8721:16 29 Nov '08  
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[Message Removed] Pinmemberstonber15:22 18 Sep '08  
GeneralUsing in Artistic Style Pinmemberjimp025:43 3 Apr '08  
GeneralGeez... Pinmemberlarryfr10:39 5 Mar '08  
GeneralConvert to a replace? Pinmemberwilliaps9:31 20 Mar '07  
GeneralC# RexExp version Pinmemberspinsane7:30 4 Nov '06  
GeneralKudos Pinmemberquantumred5:37 14 Oct '06  
Generalwildcmp("*<*>", "<field1><field2>") not working [modified] PinmemberDaniel B.14:14 6 Sep '06  
GeneralRe: wildcmp("*<*>", "<field1><field2>") not working Pinmemberradboudp1:35 16 Feb '07  
Generalreturn value type Pinmemberwdx0416:49 8 Jan '06  
General*? case match Pinmembertalimu0:42 4 Nov '05  
GeneralRe: *? case match Pinmemberkuhnm3:18 15 Sep '06  
GeneralRe: *? case match Pinmemberkuhnm5:48 18 Sep '06  
GeneralGets my 5 PinmemberFranc Morales18:05 18 Oct '05  
Generalmp and cp Pinmembertwopieman12:59 15 Mar '05  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Feb 2005
Editor: Smitha Vijayan
Copyright 2001 by Jack Handy
Everything else Copyright © CodeProject, 1999-2009
Web21 | Advertise on the Code Project