|
-
June 5th, 2008, 08:49 AM
#1
boost match question
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...
-
June 5th, 2008, 10:44 AM
#2
Re: boost match question
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:
Code:
#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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|