ワイルドカードを使用したファイル名のマッチング 質問する

ワイルドカードを使用したファイル名のマッチング 質問する

独自のファイルシステムのようなものを実装する必要があります。1つの操作はFindFirstFileです。呼び出し元が次のようなものを渡したかどうかを確認する必要があります。、sample*.cpp など。私の「ファイル システム」実装は、「ファイル名」のリストを char* の配列として提供します。

このファイル名の一致を実装する Windows 関数またはソース コードはありますか?

ベストアンサー1

'*' と '?' を使用してワイルドカード名を一致させるには、これを試してください (ブーストを回避したい場合は、std::tr1::regex を使用してください)。

#include <boost/regex.hpp>
#include <boost/algorithm/string/replace.hpp>

using std::string;

bool MatchTextWithWildcards(const string &text, string wildcardPattern, bool caseSensitive /*= true*/)
{
    // Escape all regex special chars
    EscapeRegex(wildcardPattern);

    // Convert chars '*?' back to their regex equivalents
    boost::replace_all(wildcardPattern, "\\?", ".");
    boost::replace_all(wildcardPattern, "\\*", ".*");

    boost::regex pattern(wildcardPattern, caseSensitive ? regex::normal : regex::icase);

    return regex_match(text, pattern);
}

void EscapeRegex(string &regex)
{
    boost::replace_all(regex, "\\", "\\\\");
    boost::replace_all(regex, "^", "\\^");
    boost::replace_all(regex, ".", "\\.");
    boost::replace_all(regex, "$", "\\$");
    boost::replace_all(regex, "|", "\\|");
    boost::replace_all(regex, "(", "\\(");
    boost::replace_all(regex, ")", "\\)");
    boost::replace_all(regex, "{", "\\{");
    boost::replace_all(regex, "{", "\\}");
    boost::replace_all(regex, "[", "\\[");
    boost::replace_all(regex, "]", "\\]");
    boost::replace_all(regex, "*", "\\*");
    boost::replace_all(regex, "+", "\\+");
    boost::replace_all(regex, "?", "\\?");
    boost::replace_all(regex, "/", "\\/");
}

おすすめ記事