fork(3) download
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4. #include <sstream>
  5. #include <regex>
  6.  
  7. using namespace std;
  8.  
  9. int main()
  10. {
  11. string myString1 = "+50years";
  12. string myString2 = "+50years-4months+3weeks+5minutes";
  13.  
  14. regex rg("([\\+-][0-9]+[A-Za-z]+)", regex::extended);
  15. smatch sm;
  16. while (regex_search(myString2, sm, rg)) {
  17. cout <<sm[0]<<endl;
  18. myString2 = sm.suffix().str();
  19.  
  20. stringstream ss (sm[0]);
  21.  
  22. char mathOperator;
  23. int value;
  24. string timeUnit;
  25.  
  26. ss >> mathOperator >> value >> timeUnit;
  27.  
  28. cout << "mathOperator: " << mathOperator << endl;
  29. cout << "value: " << value << endl;
  30. cout << "timeUnit: " << timeUnit << endl;
  31.  
  32. }
  33.  
  34.  
  35. }
Success #stdin #stdout 0s 3364KB
stdin
Standard input is empty
stdout
+50years
mathOperator: +
value: 50
timeUnit: years
-4months
mathOperator: -
value: 4
timeUnit: months
+3weeks
mathOperator: +
value: 3
timeUnit: weeks
+5minutes
mathOperator: +
value: 5
timeUnit: minutes