Put first boost::regex match into a string

boost-regex, c++, stdstring

Solution

I think what you're missing is that `boost::regex` is the regular expression, but it doesn't do the parsing against a given input. You need to actually use it as a parameter to `boost::regex_search` or `boost::regex_match`, which evaluate a string (or iterator pairs) against the regular expression.

static const boost::regex e("<(From )?([A-Za-z0-9_]+)>(.*?)"); 
std::string chat_input("<Darker> Hello");
boost::match_results<std::string::const_iterator> results;
if (boost::regex_match(chat_input, results, e))
{
     std::string playername = results[2];  //Get contents of the second (...)
}

Problem

Somehow, I've failed to find out, how to put only the first occurrence or regular expression to string. I can create a regex object: ``` static const boost::regex e("<(From )?([A-Za-z0-9_]+)>(.*?)"); ``` Now, I need to match `([A-Za-z0-9_]+)` to `std::string`, say `playername`. ``` std::string chat_input("<Darker> Hello"); std::string playername = e.some_match_method(chat_input, 1); //Get contents of the second (...) ``` What have I missed? What should be instead of `some_match_method` and what parameters should it take?

Original source

Related problems