The C++ wildcard pattern matching library is a simple to use and very efficient wildcard matching/globbing library. The library supports the following wildcard options:
The following is a generic implementation of an iterative algorithm for wildcard
pattern matching (aka globbing) taking into account the wildcards as
described above. The algorithm is wholly iterator based and as such can be used
to match patterns over sequences of any element type that is trivially comparable
[C++ code: globmatch.hpp]
:
Note: Compare is a policy that is used to compare the current
c from the pattern with the next data element.
Examples of such policies include case sensitive/insensitive matching,
the following are some possible implementations:
The following example demonstrates the use of the glob::match free function in testing
if the string data matches the pattern string pattern, where the defaults
of * and . are assumed for match_one_or_more and match_exatcly_one respectively.
[source: wildcardmatch_example01.cpp]
#include <cstdio>#include <string>#include "globmatch.hpp"intmain(){conststd::string data ="How now brown cow!";conststd::string pattern ="How*bro.n*cow.";if(glob::match(data, pattern)){printf("'%s' matches pattern '%s'\n", data.c_str(), pattern.c_str());}return0;}
The following example demonstrates the use of the glob::match free function in testing
if the string data matches the pattern string pattern, where the wildcard
characters are explicitly defined as being * and ? for match_one_or_more
and match_exatcly_one respectively.
[source: wildcardmatch_example02.cpp]
#include <cstdio>#include <string>#include "globmatch.hpp"intmain(){conststd::string data ="How now brown cow!";conststd::string pattern ="How*bro?n*cow?";if(glob::match(data, pattern,'*','?')){printf("'%s' matches pattern '%s'\n", data.c_str(), pattern.c_str());}return0;}
The following example demonstrates the use of the glob::match free function in testing
if the string data matches the pattern string pattern. Furthermore a
case-insensitive comparator policy is used and the defaults of * and .
are assumed for match_one_or_more and match_exatcly_one respectively.
[source: wildcardmatch_example03.cpp]
#include <cstdio>#include <string>#include "globmatch.hpp"intmain(){conststd::string data ="How now brown cow!";conststd::string pattern ="hOw*BrO.n*CoW.";if(glob::match<glob::details::cis_match>(data, pattern)){printf("'%s' matches pattern '%s'\n", data.c_str(), pattern.c_str());}return0;}
The following example demonstrates the use of the glob::match free function in testing
if the string data matches the pattern string pattern. Furthermore a
case-insensitive comparator policy is used and where the wildcard characters are explicitly
defined as being * and ? for match_one_or_more and match_exatcly_one
respectively.
[source: wildcardmatch_example04.cpp]
#include <cstdio>#include <string>#include "globmatch.hpp"intmain(){conststd::string data ="How now brown cow!";conststd::string pattern ="hOw*BrO?n*CoW?";if(glob::match<glob::details::cis_match>(data, pattern,'*','?')){printf("'%s' matches pattern '%s'\n", data.c_str(), pattern.c_str());}return0;}
The following example demonstrates the use of the glob::match free function in testing
if the std::vector of type int data matches the given pattern, where the
wildcard symbols are explicitly defined as being the integer values of 0 and -1
for match_one_or_more and match_exatcly_one respectively.
[source: wildcardmatch_example05.cpp]
#include <cstdio>#include <vector>#include "globmatch.hpp"intmain(){using T =int;conststd::vector<T> data ={1,2,3,9,8,7,4,5,6};conststd::vector<T> pattern ={1,2,-1,9,0,5,6};const T zero_or_more = T(0);const T exactly_one = T(-1);constauto result = glob::details::match_impl<glob::details::general_match<int>>(std::begin(pattern),std::end(pattern),std::begin(data ),std::end(data ), zero_or_more, exactly_one);if(result){printf("'data' vector matches 'pattern' vector\n");}return0;}