Click here to Skip to main content
Licence CPOL
First Posted 22 Jul 2007
Views 26,978
Bookmarked 12 times

String Wildcard Matching (* and ?)

By | 22 Jul 2007 | Article
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)

About the Author

M Shahid Shafiq



Pakistan Pakistan

Member



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

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThanks PinmemberBugMeNot ACCOUNT6:02 16 Jul '09  
GeneralBuggy!!! Pinmembermi-chi2:18 18 Jan '08  
GeneralRe: Buggy!!! PinmemberPatrickHe13:57 11 Jun '08  
GeneralHelpful - thank you Pinmemberaface15:37 17 Aug '07  
GeneralRe: Helpful - thank you PinmemberM Shahid Shafiq20:54 17 Aug '07  
GeneralThere was a similar article already PinmemberYuantu Huang0:56 23 Jul '07  
GeneralRe: There was a similar article already PinmemberAlexandre GRANVAUD21:24 23 Jul '07  
GeneralRe: There was a similar article already Pinmembersssw2821:20 5 Mar '11  

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.

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