2

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
}
Community
  • 1
  • 1
Stan
  • 37,207
  • 50
  • 124
  • 185

1 Answers1

2

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]);
}
usta
  • 6,699
  • 3
  • 22
  • 39