-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathextract_regex_groups.hpp
62 lines (50 loc) · 1.14 KB
/
extract_regex_groups.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef EXTRACT_REGEX_GROUPS_HPP
#define EXTRACT_REGEX_GROUPS_HPP
#include <regex>
#include <string>
#include <tuple>
#include <type_traits>
#include <boost/lexical_cast.hpp>
namespace kq
{
namespace detail
{
template<typename Tuple, size_t... Is>
void assign_regex_matches(
Tuple&& tup,
std::cmatch const& match,
std::index_sequence<Is...>
) {
using tuple_t = std::decay_t<Tuple>;
(
(void) (
std::get<Is>(tup) =
boost::lexical_cast<
std::decay_t<
std::tuple_element_t<Is, tuple_t>
>
>(match[1+Is])
), ...
);
}
}
template<typename Tuple>
void extract_regex_groups(
char const* string,
std::regex const& regex,
Tuple&& tuple
) {
constexpr auto size = std::tuple_size<Tuple>{}();
std::cmatch match;
std::regex_search(string, match, regex);
if(match.size() != size+1) {
throw std::runtime_error("Wrong number of captures");
}
detail::assign_regex_matches(
std::forward<Tuple>(tuple),
match,
std::make_index_sequence<size>{}
);
}
}
#endif // EXTRACT_REGEX_GROUPS_HPP