Possible Duplicate:
How to convert a number to string and vice versa in C++
How should I convert the boost::regex match result to other format, like integer with below code?
string s = "abc123";
boost::regex expr("(\\s+)(\\d+)");
boost::smatch match;
if(boost::regex_search(s, match, expr)) {
string text(match[0]);
// code to convert match[1] to integer
}
I'm sure you'd like to have
string text(match[1]);
// convert match[2] to integer
instead, as match[0]
is the whole matched thing (abc123 here), so submatch indexing starts at 1.
As for converting to integer part, lexical_cast is convenient to use:
string s = "abc123";
boost::regex expr("(\\s+)(\\d+)");
boost::smatch match;
if(boost::regex_search(s, match, expr)) {
string text(match[1]);
int num = boost::lexical_cast<int>(match[2]);
}
来源:https://stackoverflow.com/questions/12640284/c-to-convert-boost-regex-match-result-to-other-format