Search code examples
c++regexstringboostboost-regex

C++ multiple string grabber(regex)


I have problem with boost::regex, this solution works only for one result in each match

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

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

Solution

  • Boost.PropertyTree contains a XML parser that you can use instead of regexes:

    #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());    
    }