portability/wildcard.hpp
Platform-Independent Wildcard Matching

Introduction

This function performs the kind of wildcard matching usually found in command-line tools for filename handling. Sometimes you need to do this kind of stuff yourself. Unfortunately, this kind of thing is not always provided by the operating system. For portability, then, this function should be used instead.

Interface

The function looks like this:

bool match_wildcard(const std::string& wild, const std::string& match);

The first argument is the wildcard expression and the second is the string to match against it. The function returns true (wow, surprise) if the match string does match the wild string.

Wildcard Expressions

The wildcard expression can contain any of the following:

*
Matches 1 or more characters
?
Matches any single character (exactly 1)
[a-z]
Matches exactly 1 character in the set 'a' through 'z'. A set can contain any characters. The '-' character is used to specify a range, such as a-z, A-Z or 0-9. Special characters are escaped with a backslash '\'. This includes the ']' character which obviously closes the set, the '-' character which gives a range and the '\' character itself.
any other
Matches itself only

Thus the wildcard expression "*.vhdl" matches any string ending in the sequence ".vhdl".