Click here to Skip to main content
Click here to Skip to main content

String Wildcard Matching (* and ?)

By , 22 Jul 2007
 

Introduction

I had a problem where I wanted to match a string against a pattern based on wild characters (* and ?). The function presented in this article is a simple one that does this matching. Here are some sample patterns:

  • sam*
  • sam*e
  • samp?e
  • s*p?e
  • etc.

Using the Code

The usage of the function is very simple. Just call the function with two arguments. The first one is the string to be compared with the pattern specified in the second argument. The function does not perform any error checking.

//
//  

bool pattern_match(const char *str, const char *pattern) {
    enum State {
        Exact,      	// exact match
        Any,        	// ?
        AnyRepeat    	// *
    };

    const char *s = str;
    const char *p = pattern;
    const char *q = 0;
    int state = 0;

    bool match = true;
    while (match && *p) {
        if (*p == '*') {
            state = AnyRepeat;
            q = p+1;
        } else if (*p == '?') state = Any;
        else state = Exact;

        if (*s == 0) break;

        switch (state) {
            case Exact:
                match = *s == *p;
                s++;
                p++;
                break;

            case Any:
                match = true;
                s++;
                p++;
                break;

            case AnyRepeat:
                match = true;
                s++;

                if (*s == *q) p++;
                break;
        }
    }

    if (state == AnyRepeat) return (*s == *q);
    else if (state == Any) return (*s == *p);
    else return match && (*s == *p);
} 

History

  • 22nd July, 2007: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

M Shahid Shafiq

Pakistan Pakistan
No Biography provided

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

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
AnswerString wildcard matching (* and ?) [modified]membertmowhrekf12-Mar-13 17:59 
I was looking for something like this but didn't think much of this code.
 
After spending some time considering how to implement wildcard matching properly I came up with the following:
// wildcard matching algorithm
// written 2013 by Mathias Karlsson <tmowhrekf@gmail.com>
// use freely.

bool wildcard(const char *str, const char *pattern);
 
bool wildcard(const char *s, const char *p)
{
	// ? matches one character
	// * matches zero or more characters
	while (*s) {
		if (!*p) return false;
		if (*p == '*') {
			while (*p == '*') p++;
			if (!*p) return true;
			while (*s) if (wildcard(s++, p)) return true;
			return false;
		}
		if (*s != *p && *p != '?') return false;
		p++;
		s++;
	}
	while (*p == '*') p++;
	return (!*p);
}
If you have examples where it would fail then please reply to this or send me a message.
 
Of course, if there are security concerns you should limit the string lengths and the call stack.
 
Hopefully this code will help someone.

modified 31-Mar-13 13:59pm.

GeneralRe: String wildcard matching (* and ?)memberazorius24-Mar-13 1:10 
GeneralRe: String wildcard matching (* and ?) [modified]membertmowhrekf24-Mar-13 2:32 
GeneralThanksmemberBugMeNot ACCOUNT16-Jul-09 6:02 
GeneralBuggy!!!membermi-chi18-Jan-08 2:18 
GeneralRe: Buggy!!!memberPatrickHe11-Jun-08 13:57 
GeneralHelpful - thank youmemberaface17-Aug-07 15:37 
GeneralRe: Helpful - thank youmemberM Shahid Shafiq17-Aug-07 20:54 
GeneralThere was a similar article alreadymemberYuantu Huang23-Jul-07 0:56 
GeneralRe: There was a similar article alreadymemberAlexandre GRANVAUD23-Jul-07 21:24 
GeneralRe: There was a similar article alreadymembersssw285-Mar-11 21:20 

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

Permalink | Advertise | Privacy | Mobile
Web04 | 2.6.130619.1 | Last Updated 22 Jul 2007
Article Copyright 2007 by M Shahid Shafiq
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid