C ++将Boost Regex匹配结果转换为其他格式 [英] C++ to convert Boost Regex match result to other format

查看:215
本文介绍了C ++将Boost Regex匹配结果转换为其他格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:


$ b $

b


如何将boost :: regex匹配结果转换为其他格式,如下面的整数代码?

  string s =abc123; 
boost :: regex expr((\\s +)(\\d +));
boost :: smatch match;
if(boost :: regex_search(s,match,expr)){
string text(match [0]);
//将match [1]转换为整数的代码
}


解决方案

我确定你想要

  ); 
//将match [2]转换为整数

> match [0] 是整个匹配的东西(abc123这里),所以submatch索引从1开始。



lexical_cast 方便使用:

  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]);
}


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]);
}

这篇关于C ++将Boost Regex匹配结果转换为其他格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆