RegexMatch template
Marcus
41
Reputation points
I'm trying to convert this function to a template.
std::vector<std::smatch> RegexMatchAll(const std::string& str, std::string regex)
{
std::vector<std::smatch> result;
std::regex re(regex);
auto words_begin = std::sregex_iterator(str.begin(), str.end(), re);
auto words_end = std::sregex_iterator();
for (std::sregex_iterator i = words_begin; i != words_end; ++i) {
std::smatch match = *i;
result.emplace_back(match);
}
return result;
}
So far what I have already done:
template <typename T>
auto RegexMatchAll(const std::basic_string<T>& str, const std::basic_string<T>& regex)
{
using iter = typename std::basic_string<T>::const_iterator;
std::basic_regex<T> re(regex);
std::vector<std::match_results<iter>> result;
auto words_begin = iter(str.begin(), str.end(), re);
auto words_end = iter();
for (iter i = words_begin; i != words_end; ++i) {
std::match_results<iter> m = *i;
result.emplace_back(m);
}
return result;
}
I'm getting some errors like:
<function-style-cast>': cannot convert from 'initializer list' to 'iter
'words_begin': cannot be used before it is initialized
And I'm calling the template as:
auto matches = RegexMatchAll(str, R"(...)"s);
auto matches = RegexMatchAll(wstr, LR"(...)"s);