Skip to content

Instantly share code, notes, and snippets.

@mrowrpurr
Created February 17, 2023 05:26
Show Gist options
  • Save mrowrpurr/4ffe08a3201d954b11d30b85e27187ac to your computer and use it in GitHub Desktop.
Save mrowrpurr/4ffe08a3201d954b11d30b85e27187ac to your computer and use it in GitHub Desktop.
class MatchesRegexMatcher {
std::vector<std::string> _patterns;
public:
template <typename... Args>
MatchesRegexMatcher(std::string pattern, Args... args) : _patterns({pattern, args...}) {}
const std::vector<std::string>& GetPatterns() const { return _patterns; }
bool Matches(const std::string& text) const {
for (const auto& pattern : _patterns) {
if (!std::regex_search(text, std::regex(pattern, std::regex_constants::icase))) {
return false;
}
}
return true;
}
friend std::ostream& operator<<(std::ostream& os, const MatchesRegexMatcher& matcher) {
if (matcher._patterns.size() == 1) {
return os << "match regular expression /" << matcher._patterns[0] << "/";
} else {
os << "match regular expressions:";
for (const auto& pattern : matcher._patterns) {
os << " /" << pattern << "/";
}
return os;
}
}
};
#define MatchesRegex(...) Fulfills(MatchesRegexMatcher(__VA_ARGS__))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment