Why is my regex capture not working properly

Viewed 28

I am using the below code:

const std::regex sBlockSplit("([\\w]+)[\\n]*");
std::smatch matches;
std::regex_search(str, matches, sBlockSplit);
for (size_t i = 0; i < matches.size(); ++i) {
     std::cout << "'" << matches[i] << "'" << ", ";
}
std::cout << "\n";

with the includes:

#include <SDL.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include <SDL_Image.h>
#include <fstream>
#include <regex>

To capture some individual lines of a file, with a regex "([\w]+)[\n]*"

It works fine in Regexr.com, with the test file

foo
bar
foo_bar

and captures "foo", "bar", and "foo_bar". However, running this in my c++ program with the same input, it instead writes "'foo'\n, 'foo'," (Note that it's not capturing anything else, that trailing comma is just because I wrote the debug line poorly)

Why is this happening? Have I written my regex incorrectly, screwed something up in code, or is c++ not working on the proper version of regex?

0 Answers
Related