views:

43

answers:

1

My code is:

#include <boost/regex.hpp>
boost::cmatch matches;
boost::regex_match("alpha beta", matches, boost::regex("([a-z])+"));
cout << "found: " << matches.size() << endl;

And it shows found: 2 which means that only ONE occurrence is found… How to instruct it to find THREE occurrences? Thanks!

A: 

This is what I've found so far:

text = "alpha beta";
string::const_iterator begin = text.begin();
string::const_iterator end = text.end();
boost::match_results<string::const_iterator> what;
while (regex_search(begin, end, what, boost::regex("([a-z]+)"))) {
    cout << string(what[1].first, what[2].second-1);
    begin = what[0].second;
}

And it works as expected. Maybe someone knows a better solution?

Vincenzo