// modified from: http://onlamp.com/pub/a/onlamp/2006/04/06/boostregex.html?page=3 #include #include #include int main( ) { while (true) { std::string expr, s; std::cout << "Expression: "; std::getline(std::cin, expr); if (!std::cin || expr == "quit") return 0; std::cout << "String: "; std::getline(std::cin, s); try { std::smatch matches; if (std::regex_match(s, matches, std::regex(expr))) { // matches[0] contains the original string. // matches[n] contains a sub_match object for each matching subexpression for (size_t i = 0; i < matches.size(); i++) { // sub_match is pair of iterators to the first // and one past the last chars of the matching subexpression // (matches[i].first, matches[i].second); std::string match = matches[i]; std::cout << "\tmatches[" << i << "] = " << match << '\n'; } } else std::cout << "The regexp \"" << expr << "\" does not match \"" << s << "\"\n"; } catch (std::regex_error& e) { std::cout << expr << " is not a valid regular expression: \"" << e.what() << "\"\n"; } } return 0; }