#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main( ) {
const auto input = "Some people, when confronted with a problem, think \" I know, I'll use regular expressions.\" " s;
smatch sm;
cout << input << endl;
// If input ends in a quotation that contains a word that begins with "reg" and another word begining with "ex" then capture the preceeding portion of input
if ( regex_match( input, sm, regex( "(.*)\" .*\\ breg.*\\ bex.*\" \\ s*$" ) ) ) {
const auto capture = sm[ 1 ] .str ( ) ;
cout << '\t ' << capture << endl; // Outputs: "\tSome people, when confronted with a problem, think\n"
// Search our capture for "a problem" or "# problems"
if ( regex_search( capture, sm, regex( "(a|d+)\\ s+problems?" ) ) ) {
const auto count = sm[ 1 ] == "a" s ? 1 : stoi( sm[ 1 ] ) ;
cout << '\t ' << count << ( count > 1 ? " problems\n " : " problem\n " ) ; // Outputs: "\t1 problem\n"
cout << "Now they have " << count + 1 << " problems.\n " ; // Ouputs: "Now they have 2 problems\n"
}
}
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cmVnZXg+CiNpbmNsdWRlIDxzdHJpbmc+Cgp1c2luZyBuYW1lc3BhY2Ugc3RkOwoKaW50IG1haW4oKSB7Cgljb25zdCBhdXRvIGlucHV0ID0gIlNvbWUgcGVvcGxlLCB3aGVuIGNvbmZyb250ZWQgd2l0aCBhIHByb2JsZW0sIHRoaW5rIFwiSSBrbm93LCBJJ2xsIHVzZSByZWd1bGFyIGV4cHJlc3Npb25zLlwiInM7CglzbWF0Y2ggc207CgkKCWNvdXQgPDwgaW5wdXQgPDwgZW5kbDsKCgkvLyBJZiBpbnB1dCBlbmRzIGluIGEgcXVvdGF0aW9uIHRoYXQgY29udGFpbnMgYSB3b3JkIHRoYXQgYmVnaW5zIHdpdGggInJlZyIgYW5kIGFub3RoZXIgd29yZCBiZWdpbmluZyB3aXRoICJleCIgdGhlbiBjYXB0dXJlIHRoZSBwcmVjZWVkaW5nIHBvcnRpb24gb2YgaW5wdXQKCWlmIChyZWdleF9tYXRjaChpbnB1dCwgc20sIHJlZ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