fork(1) download
  1. #include <iostream>
  2. #include <regex>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. int main() {
  8. const auto input = "Some people, when confronted with a problem, think \"I know, I'll use regular expressions.\""s;
  9. smatch sm;
  10.  
  11. cout << input << endl;
  12.  
  13. // 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
  14. if (regex_match(input, sm, regex("(.*)\".*\\breg.*\\bex.*\"\\s*$"))) {
  15. const auto capture = sm[1].str();
  16.  
  17. cout << '\t' << capture << endl; // Outputs: "\tSome people, when confronted with a problem, think\n"
  18.  
  19. // Search our capture for "a problem" or "# problems"
  20. if(regex_search(capture, sm, regex("(a|d+)\\s+problems?"))) {
  21. const auto count = sm[1] == "a"s ? 1 : stoi(sm[1]);
  22.  
  23. cout << '\t' << count << (count > 1 ? " problems\n" : " problem\n"); // Outputs: "\t1 problem\n"
  24. cout << "Now they have " << count + 1 << " problems.\n"; // Ouputs: "Now they have 2 problems\n"
  25. }
  26. }
  27. }
Success #stdin #stdout 0s 3500KB
stdin
Standard input is empty
stdout
Some people, when confronted with a problem, think "I know, I'll use regular expressions."
	Some people, when confronted with a problem, think 
	1 problem
Now they have 2 problems.