Click to See Complete Forum and Search --> : boost match question


pukemiketukas
June 5th, 2008, 08:49 AM
Ok. I'm developing small c++ program which should parse my xml files. This is a part of the script:

string xmlData = "test<sessionTest>10</sessionTest>test";
boost::regex pat("(.*)<sessionTest>(.*)</sessionTest>(.*)");

boost::smatch matches;
if (boost::regex_match(xmlData, matches, pat))
cout << matches[2] << endl;

And the script return me 10, but I need to assign matches[2] to an integer variable. How I can do that. I try to assign directly and with atoi() but i'v got no lucky so far.

I'm using linux boost_regex library.

Ps: is there any better way to write pattern in c++ to parse data inside <sessionTest>?

Thanks in advance...

Duoas
June 5th, 2008, 10:44 AM
Your pattern is fine. Just keep in mind that regex works on strings, nothing else.

You'll need to convert the pattern. Don't use atoi(). Instead use a temporary stringstream:

#include <boost/regexp.hpp>
#include <sstream>
#include <string>

...


// play with regex's here

int match_value;
{
stringstream ss( matches[2] );
if (!(ss >> match_value))
fooey( "the item was not an integer" );
}

// use match_value here


Hope this helps.