Stephen K
Stephen K

Reputation: 737

regex search matches not using group

Have the following code:

boost::regex CriticalHit("<c=#f12d2d>(.+)</c>", boost::regex::icase);

idea is to match everything in

else if (boost::regex_search(text, damage, CriticalHit)) {
    for (int i = 0; i < damage.size(); i++) {
        HL_LOG_ERR("%s\n", damage[i]);
    }
}

this will output varying strings starting at the <c=#... part. damage[0] gives the whole string, damage[1] gives starting at the capture group and then the rest of the string.

The whole string looks like "You critically hit for <c=#399999>5,992</c>"

What am I doing wrong?

Upvotes: 0

Views: 63

Answers (1)

Pete Becker
Pete Becker

Reputation: 76315

Nobody here knows what HI_LOG_ERROR does. Use standard streams and inserters. As a guess, if the %s is part of a C-style format string, then you have to convert damage[i] to a C-style string. damage[i] is a sub_match object, which is essentially two iterators. To convert it to a C-style string, first use its conversion operator to get a C++ string, then use .c_str() to get a C string:

HI_LOG_ERROR("%s\n", std::string(damage[i]).c_str());

Upvotes: 2

Related Questions