为非结构化字符串提升regex_search

时间:2011-05-12 20:31:51

标签: c++ regex boost

你好了解完这个主题的现有标签后还没找到任何东西,我想我会在这里问一下。基本上我是regex和C ++的新手,但我试图将一些遗留代码移植到C ++。我们非常感谢任何有关文学阅读的帮助。

我有一个模式字符串,用于搜索字符串中的"Life\\s*Policy\\s*=\\s*(([0-9]+)|[0-9]+\\.[0-9]*);",该字符串为“Life Policy = 9.67; Life Benefits = 1000; Life Claim = 100”

如何使用regex_search函数获取值9.67,以便我可以在计算中进一步使用它?

我目前有这个代码,我无法提取我想要的内容:

std::string::const_iterator begin;
boost::match_results<std::string::const_iterator> what;
begin = st.begin();
boost::match_flag_type flags = boost::match_perl;

while(boost::regex_search(begin,st.end(),what,ex,flags))
{
    successcode = 200;
}

我的想法是拥有我需要的所有信息。但是what[0].first将我指向匹配的字符串的开头...即Life Policy = 9.67;Life Claims = ..

1 个答案:

答案 0 :(得分:2)

what[0]匹配整个字符串。 what[1]表示第一个匹配,what[2]代表第二个匹配,依此类推。