C++ to convert Boost Regex match result to other format [duplicate]

陌路散爱 提交于 2019-12-06 05:49:56

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]);
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!