[英]C++ multiple string grabber(regex)
我在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;
現在解析字符串
<x id="first">
<value>5</value>
</x>
<x id="second">
<value>56</value>
</x>
etc...
現在的問題是如何立即解析id和value以在match循環中同時獲取它們
for( ; iter != end; ++iter ) {
std::string id(iter->first, iter->second);
std::string value(?????);
}
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.