简体   繁体   English

C ++多字符串采集器(regex)

[英]C++ multiple string grabber(regex)

I have problem with boost::regex, this solution works only for one result in each match 我在boost :: regex上遇到问题,此解决方案仅在每次比赛中都针对一个结果

boost::regex regex("id=\"(.*?)\""); // should I use this "id=\"(.*?)\"(.*?)<value>(.*?)</value>"?
boost::sregex_token_iterator iter(xml.begin(), xml.end(), regex, 1); // 1 because I just need text inside quotes
boost::sregex_token_iterator end;

and now parsed string 现在解析字符串

<x id="first">
<value>5</value>
</x>
<x id="second"> 
<value>56</value>  
</x>  
etc... 

Now question is how to parse id and value at once to grab them both inside matches loop 现在的问题是如何立即解析id和value以在match循环中同时获取它们

for( ; iter != end; ++iter ) {
  std::string id(iter->first, iter->second);
  std::string value(?????);
}

Boost.PropertyTree contains a XML parser that you can use instead of regexes: Boost.PropertyTree包含一个XML解析器,您可以使用它代替正则表达式:

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include <boost/foreach.hpp>
...    
using boost::property_tree::ptree;
ptree pt;
read_xml(istreamOrFilename, pt);
BOOST_FOREACH(ptree::value_type &v, pt) {
    std::string id(v.second.get<std::string>("<xmlattr>.id"));
    std::string value(v.second.get<std::string>("value").data());    
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM