Pulling submatches out of a match

To use regexes for the lexing stage of input, you'll need a way to pull out the input substrings that matched each capturing group. The way you do this in C++ is by creating a match object of type std::smatch. No, that's not a typo! The name of the match-object type really is smatch, which stands for std::string match; there is also cmatch for const char * match. The difference between smatch or cmatch is the type of iterator they store internally: smatch stores string::const_iterator, while cmatch stores const char *.

Having constructed an empty std::smatch object, you'll pass it by reference as the middle parameter to regex_match or regex_search. Those functions will "fill in" the smatch object with information ...

Get Mastering the C++17 STL now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.