Iterating over multiple matches
Consider the regex (?!\d)\w+
, which matches a single C++ identifier. We already know how to use std::regex_match
to tell whether an input string is a C++ identifier, and how to use std::regex_search
to find the first C++ identifier in a given input line. But what if we want to find all the C++ identifiers in a given input line?
The fundamental idea here is to call std::regex_search
in a loop. This gets complicated, though, because of the non-consuming "lookbehind" anchors such as ^
and \b
. To implement a loop over std::regex_search
correctly from scratch, we'd have to preserve the state of these anchors. std::regex_search
(and std::regex_match
for that matter) supports this use-case by providing flags of its own--flags which determine the starting state of the finite state machine for this particular matching operation. For our purposes, the only important flag is std::regex::match_prev_avail
, which tells the library that the iterator begin
, representing the...