CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jun 2008
    Posts
    1

    Angry 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...

  2. #2
    Join Date
    May 2008
    Posts
    96

    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
  •  





Click Here to Expand Forum to Full Width

Featured