17 using svmatch = std::match_results<std::string_view::const_iterator>;
19 template<
class B
idirIt,
class UnaryFunction>
20 [[nodiscard]] std::string regex_replace(BidirIt&& first, BidirIt&& last,
const std::regex& re, UnaryFunction&& f)
24 typename std::match_results<BidirIt>::difference_type positionOfLastMatch = 0;
26 auto endOfLastMatch = first;
27 std::for_each(std::regex_iterator<BidirIt>{first, last, re}, {}, [&](
const std::match_results<BidirIt>& match) {
28 auto positionOfThisMatch = match.position(0);
29 auto diff = positionOfThisMatch - positionOfLastMatch;
31 auto startOfThisMatch = endOfLastMatch;
32 std::advance(startOfThisMatch, diff);
34 s.append(endOfLastMatch, startOfThisMatch);
37 auto lengthOfMatch = match.length(0);
39 positionOfLastMatch = positionOfThisMatch + lengthOfMatch;
41 endOfLastMatch = startOfThisMatch;
42 std::advance(endOfLastMatch, lengthOfMatch);
45 s.append(endOfLastMatch, last);
50 template<
class UnaryFunction>
51 [[nodiscard]] std::string regex_replace(std::string_view s,
const std::regex& re, UnaryFunction&& f)
53 return regex_replace(s.cbegin(), s.cend(), re, std::forward<UnaryFunction>(f));
56 template <
class B
idirIt,
class UnaryFunction>
57 void regex_split(BidirIt first, BidirIt last,
const std::regex& re, UnaryFunction&& f)
59 std::for_each(std::regex_token_iterator<BidirIt>{first, last, re, -1}, {}, std::forward<UnaryFunction>(f));
62 template <
class UnaryFunction>
63 void regex_split(std::string_view s,
const std::regex& re, UnaryFunction&& f)
65 regex_split(s.begin(), s.end(), re, std::forward<UnaryFunction>(f));
68 [[nodiscard]]
inline std::vector<std::string> regex_split(std::string_view s,
const std::regex& re)
70 return resulting([&](std::vector<std::string>& result) {
71 regex_split(s.begin(), s.end(), re, op::push_back_to(result));