5,421,850 members and growing! (18,299 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.
VC6, C++Windows, NT4, Win2K, Visual Studio, Dev

Posted: 1 May 2001
Updated: 15 Feb 2005
Views: 243,405
Bookmarked: 53 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
91 votes for this Article.
Popularity: 8.82 Rating: 4.50 out of 5
5 votes, 7.9%
1
0 votes, 0.0%
2
1 vote, 1.6%
3
5 votes, 7.9%
4
52 votes, 82.5%
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



Occupation: Web Developer
Location: United States United States

Other popular String handling articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 25 of 106 (Total in Forum: 106) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralUsing in Artistic Stylememberjimp025:43 3 Apr '08  
GeneralGeez...memberlarryfr10:39 5 Mar '08  
GeneralConvert to a replace?memberwilliaps9:31 20 Mar '07  
GeneralC# RexExp versionmemberspinsane7:30 4 Nov '06  
GeneralKudosmemberquantumred5:37 14 Oct '06  
Generalwildcmp("*<*>", "<field1><field2>") not working [modified]memberDaniel B.14:14 6 Sep '06  
GeneralRe: wildcmp("*<*>", "<field1><field2>") not workingmemberradboudp1:35 16 Feb '07  
Generalreturn value typememberwdx0416:49 8 Jan '06  
General*? case matchmembertalimu0:42 4 Nov '05  
GeneralRe: *? case matchmemberkuhnm3:18 15 Sep '06  
GeneralRe: *? case matchmemberkuhnm5:48 18 Sep '06  
GeneralGets my 5memberFranc Morales18:05 18 Oct '05  
Generalmp and cpmembertwopieman12:59 15 Mar '05  
GeneralRe: mp and cpmemberradboudp2:14 16 Feb '07  
GeneralOK, but ...memberSam Levy5:48 16 Feb '05  
GeneralWhy make 3 loop ?memberDarkYoda Mickael23:22 2 Feb '05  
GeneralRe: Why make 3 loop ?memberJack Handy11:02 13 Feb '05  
GeneralC# versionmemberSancy7:23 26 Oct '04  
GeneralRe: C# versionsussPsyk664:39 21 Dec '04  
GeneralRe: C# versionmemberIonut FIlip7:15 22 Feb '05  
GeneralRe: C# versionmemberrobagar17:58 3 Apr '06  
GeneralRe: C# versionmemberSancy17:01 5 Jun '06  
GeneralConvert to java base on C# version [modified, better look :~ ]memberquangtin322:13 21 Mar '08  
GeneralRe: C# version - an error!memberMark T.15:37 4 Jul '08  
GeneralMany thanks, with 1 small gripe ..memberDavid Patrick9:41 29 Sep '04  

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-2008
Web19 | Advertise on the Code Project