0

I want to concatenate all matches found by regex_search into a single string, and then return it. I tried doing it with std::accumulate, but failed.

Is there a way to return something like std::accumulate(what.begin()+1, what.end(), someFunc)?
I'm not very familiar with functional programming. I know that I can make a for loop that adds strings together, but I want to try doing it otherwise. Thanks in advance!

Here is a pseudo-code snippet that might help you better understand what I want to do.

std::string foo(const std::string& text) 
{   
    using namespace boost::xpressive;
    sregex srx = +_d >> as_xpr("_") >> +_d; // some random regex
    smatch what;

    if (regex_search(filename, what, srx))
    {
        // Here I want to return a string,
        //  concatenated from what[1].str() + what[2].str() + ... + what[n].str();
        // How do I do this?
        // What about what[1].str() + "-" + what[2].str()...?
    }
    return std::string();
}
yacc
  • 2,915
  • 4
  • 19
  • 33
Eugene Mart
  • 185
  • 1
  • 10
  • When it failed, what was your error? What was your input and expected output? A [mcve] can really help us get you where you want. – AndyG May 30 '19 at 11:59
  • I failed to make a someFunc from std::accumulate(what.begin()+1, what.end(), someFunc). Have no idea how it should look like. Definitely something like [](const sometype& first, const sometype& second) -> std::string { return first.str() + "-" + second.str(); }, but I don't know what this "sometype" should be. – Eugene Mart May 30 '19 at 12:02

0 Answers0