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

我只是一个虾纸丫 提交于 2019-12-10 11:10:01

问题


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
}

回答1:


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

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