In Ruby I can say:
"one two buckle my shoe ".gsub(/(.*?) /) {|m| p m}
And get the expected result (each word is printed to its own line).
I'm trying to do the same in C++, but it's being dippy:
#include <string>
#include <regex>
#include <iostream>
using namespace std;
int main() {
string sentence("one two buckle my shoe ");
regex exp("(.*?) ");
smatch words;
regex_search(sentence, words, exp);
for(auto word : words) {cout << "\"" << word << "\"" << endl;}
system("pause");
return 0;
}
It produces:
"one " //why is it not matching the whole string?
"one"
Whereas I want it to produce:
"one two buckle my shoe " //I'll discard this annoying false match one the problem is solved
"one"
"two"
"buckle"
"my"
"shoe"
Can anyone see what I'm doing wrong here?