More fun with std::regex

Started by
0 comments, last by Khatharr 9 years, 11 months ago

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?

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.
Advertisement

Never mind. I found the problem. Apparently regex_search() only grabs one match and any submatched groups. You have to re-search the suffix string to get more matches. -.-

void hurrrrrrrr() {__asm sub [ebp+4],5;}

There are ten kinds of people in this world: those who understand binary and those who don't.

This topic is closed to new replies.

Advertisement