#include #include #include #include #include #include int main( ) { const std::string xmltext( "" "
" "" "Invisibility Cream" "14.50" "Makes you invisible" "" "" "Levitation Salve" "23.99" "Levitate yourself for up to 3 hours per application" "" "
" "
" "" "Blork and Freen Instameal" "4.95" "A tasty meal in a tablet; just add water" "" "" "Grob winglets" "3.56" "Tender winglets of Grob. Just add water" "" "
" "
" ) ; std::string::size_type found = xmltext.find( "" , found + 5 ) ; //and its end std::cout << "The first item is\n" << xmltext.substr( found + 5 , foundnext - ( found + 5 ) ) << '\n' ; std::string::const_iterator start , end ; start = xmltext.begin( ) ; end = xmltext.end( ) ; boost::match_results what ; boost::regex pricefind( "(\\d+\\.?\\d+)" ) ;//this regex finds the prices start = xmltext.begin( ) ; std::cout << "The prices are:\n" ; while ( boost::regex_search( start , end , what , pricefind ) ) { std::string price( what[ 1 ].first , what[ 1 ].second ) ;//find the first price std::cout << price << std::endl ; start = what[ 1 ].second ; //continue search after first price found } start = xmltext.begin( ) ; std::vector names ; boost::regex namefind( "(.+?)" ) ; //find characters, be greedy! while ( boost::regex_search ( start , end , what , namefind ) ) { std::string name ( what[ 1 ].first , what[ 1 ].second ) ; names.push_back( name ) ; start = what[ 1 ].second ; } std::cout << "The following name elements were found in the xml string:\n" ; std::copy( names.begin( ) , names.end( ) , std::ostream_iterator( std::cout , "\n" )) ; return 0 ; }