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 the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.