A few things here.
First, your regex string must have escape code \ . Its still C ++ - a string, after all.
regex rgx("\\w");
In addition, the regular expression \w matches only one character of the word. If you want to combine the whole word, you need to use:
regex rgx("\\w+");
Finally, to iterate over all possible matches, you need to use an iterator. Here is a complete working example:
#include <regex> #include <string> #include <iostream> using namespace std; int main() { string seq = "Some words. And... some punctuation."; regex rgx("\\w+"); for( sregex_iterator it(seq.begin(), seq.end(), rgx), it_end; it != it_end; ++it ) cout << (*it)[0] << "\n"; }
source share