Click here to Skip to main content
Click here to Skip to main content
Go to top

String Wildcard Matching (* and ?)

, 22 Jul 2007
Rate this:
Please Sign up or sign in to vote.
A simple function to perform wildcard ( * ? ) string matching

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)

Share

About the Author

M Shahid Shafiq

Pakistan Pakistan
No Biography provided

Comments and Discussions

 
Generalbug find PinmemberSudhanshu Pratap Singh16-Sep-13 12:20 
AnswerString wildcard matching (* and ?) [modified] Pinmembertmowhrekf12-Mar-13 17:59 
GeneralRe: String wildcard matching (* and ?) Pinmemberazorius24-Mar-13 1:10 
GeneralRe: String wildcard matching (* and ?) [modified] Pinmembertmowhrekf24-Mar-13 2:32 
You are right. I have modified the above example to help the compiler (which couldn't know this).
 
However, if you are really trying to optimize for speed there would be faster methods for most purposes (precompiling the pattern, recognizing common pattern formats, maybe using faster stacks instead of recursive calls, etc.) but the code here is visible and accurate as an algorithm.

modified 24-Mar-13 11:06am.

GeneralThanks PinmemberBugMeNot ACCOUNT16-Jul-09 6:02 
GeneralBuggy!!! Pinmembermi-chi18-Jan-08 2:18 
GeneralRe: Buggy!!! PinmemberPatrickHe11-Jun-08 13:57 
GeneralHelpful - thank you Pinmemberaface17-Aug-07 15:37 
GeneralRe: Helpful - thank you PinmemberM Shahid Shafiq17-Aug-07 20:54 
GeneralThere was a similar article already PinmemberYuantu Huang23-Jul-07 0:56 

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.

| Advertise | Privacy | Mobile
Web03 | 2.8.140922.1 | Last Updated 22 Jul 2007
Article Copyright 2007 by M Shahid Shafiq
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid