Click here to Skip to main content
15,881,709 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

 
GeneralC# RexExp version Pin
spinsane4-Nov-06 6:30
spinsane4-Nov-06 6:30 
Here's RegExp version (may be easily ported to C++).
Pros: More readable, Relies on proven RegExp
Cons: Maybe slower?, If eval string contains RegExp keywords then it might result in unexpected result

<br />
public static bool Match(string eval, string pattern, bool caseSensitive)<br />
{<br />
	bool match = false;<br />
<br />
	// Make input parameters lower-case if case is not an issue<br />
	if (!caseSensitive)<br />
	{<br />
		eval = eval.ToLower();<br />
		pattern = pattern.ToLower();<br />
	}<br />
<br />
	// Escape regexp special character in pattern<br />
	pattern = pattern.Replace(".", @"\.");<br />
<br />
	// Replace valid wildcards with regexp equivalents<br />
	pattern = pattern.Replace('?', '.').Replace("*", ".*");<br />
<br />
	// Add boundaries to pattern<br />
	pattern = @"\A" + pattern + @"\z";<br />
<br />
	// Search for a match<br />
	try<br />
	{<br />
		match = Regex.IsMatch(eval, pattern);<br />
	}<br />
	catch /* (ArgumentException ex) */<br />
	{<br />
		// Syntax error in the regular expression<br />
	}<br />
<br />
	// Return result<br />
	return match;<br />
}<br />

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.