Created
February 5, 2017 02:16
-
-
Save jtbandes/0c3f7d65fae6adbb36364db82468cbb7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <regex> | |
template<typename StringT> | |
struct regex_searcher_t { | |
using IterT = decltype(std::begin(std::declval<const StringT&>())); | |
// using CharT = typename std::decay<decltype(*std::declval<IterT>())>::type; | |
using CharT = typename std::iterator_traits<IterT>::value_type; | |
const std::basic_regex<CharT> _r; | |
const std::regex_iterator<IterT> _it; | |
regex_searcher_t(const StringT& s, const std::basic_regex<CharT>& r) : _r(r), _it(std::begin(s), std::end(s), _r) {} | |
decltype(_it) begin() { | |
return _it; | |
} | |
decltype(_it) end() { | |
return decltype(_it)(); | |
} | |
}; | |
template<typename StringT> | |
regex_searcher_t<StringT> regex_searcher(const StringT& s, const std::basic_regex<typename std::decay<decltype(*std::begin(std::declval<StringT&>()))>::type>& r) { | |
return regex_searcher_t<StringT>(s, r); | |
} | |
int main(int argc, const char **argv) { | |
// std::cmatch m; | |
// if (std::regex_search("foobar", m, std::regex(R"(..)"))) { | |
// std::cout << "m[0] = " << m[0] << std::endl; | |
// } | |
// const char *foo = "abcdef"; | |
// std::regex r(R"(..)"); | |
// std::regex_iterator<char*>(foo, foo+strlen(foo), r); | |
std::string ss = "foobar"; | |
for (const auto& m : regex_searcher(ss, std::regex(R"(..)"))) { | |
std::cout << "found match m[0] = " << m[0] << std::endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment