Saturday, February 19, 2011

boost spirit extracting first word and store it in a vector

Hi,

I have problems with Boost.Spirit parsing a string.

The string looks like

name1 has this and that.\n 
name 2 has this and that.\n 
na me has this and that.\n

and I have to extract the names. The text "has this and that" is always the same but the name can consist of spaces therefore I can't use graph_p.

1) How do I parse such a string?

Since the string has several lines of that format I have to store the names in a vector.

I used something like

std::string name;
rule<> r = *graph_p[append(name)];

for saving one name but

2) what's the best way to save several names in a vector?

Thanks in advance

Konrad

From stackoverflow
  • I presume there is a reason why you are using Boost.Spirit and not STL's string's find method? E.g:

    string s = "na me has this and that.\n";
    myVector . push_back( s.substr( 0, s.find( "has this and that" ) ) );
    
  • I think this will do the trick:

    vector<string> names;
    string name;
    parse(str,
        *(  
           (*(anychar_p - "has this and that.")) [assign_a(name)]
           >> "has this and that.\n") [push_back_a(names, name)]
         ))
    
    BenoƮt : +1. Clean and simple !
  • vector<string> names;
    parse(str,
        *(  
           (*(anychar_p - "has this and that.")) [push_back_a(names)]
           >> "has this and that.\n")
         ))
    

    Hi,

    thanks for your response. I have some problems with this version because there are following lines which have a completely different format and they are also parsed as true because there obviously is no "has this and that.". So I changed it to

    vector<string> names;
    parse(str,
        *(  
           (*(anychar_p - "has this and that.") >> "has this and that.\n"))[push_back_a(names)]      
         ))
    

    Now it parses the lines right but I got a new problem with the push_back actor because now it is pushing the complete line " has this and that" into the vector. Is it possible to remove the "has this and that. " before pushing it back or do I have to manually edit the vector afterwards?

0 comments:

Post a Comment