// Uppgift: Del 1: Hitta while-loopar och dess villkor. Del 2: Läs in text och identifiera alla inbäddade tal. // Nyckelord: boost::regex (std::regex), raw-string-literal #include // C++11 std::regex kompilerar med g++, men fungerar ännu inte att använda //#include //using namespace std; // Installation: sudo apt-get install libboost-all-dev // Kompilering: g++ -std=c++11 -lboost_regex #include using namespace boost; using std::cin; using std::cout; using std::endl; using std::string; int main() { // Del 1 { cout << "Mata in tecken: " << endl; // A raw string start with R"( and end with )" in g++ regex r( R"(^\s*while\s*\((.*)\)\s*$)" ); string input; while (getline(cin, input)) { smatch m; if (regex_match(input, m, r)) { smatch::const_iterator i = m.begin(); cout << "Entire match (while): " << *i++ << endl; cout << "Capture (expression): " << *i << endl; } } } cin.clear(); // Del 2 { cout << "Mata in tecken igen: " << endl; regex r("[0-9]+"); string input; while (cin >> input) { smatch m; while (regex_search(input, m, r)) { for (smatch::const_iterator i = m.begin(); i != m.end(); ++i) { cout << *i << endl; } input = m.suffix(); } } } cout << endl; return 0; }