[spirit][qi] How to write a boost::spirit::qi parser to do what '?' does in regex?

classic Classic list List threaded Threaded
2 messages Options
Han
Reply | Threaded
Open this post in threaded view
|

[spirit][qi] How to write a boost::spirit::qi parser to do what '?' does in regex?

Han
Let's say we have a regex "start:(?: ([0-9]{1,2}))? ([0-9].*)".

It will match
    std::string string1 = "start: 01 0ab";

and
    std::string string2 = "start: 0ab";

We can also get the 2 matched string respectively.

I try to use boost::spirit::qi parser to parse string2 but it couldn't
match.
qi::rule<std::string::const_iterator, std::string()> rule1 =
qi::repeat(1,2)[qi::digit];
qi::rule<std::string::const_iterator, std::string()> rule2 = qi::digit >>
*qi::char_;
std::vector<std::string> attr;
auto it_begin = string2.begin();
auto it_end = string2.end();
if (qi::parse(
         it_begin,
         it_end,
         qi::lit("start:")
             >> -(qi::lit(" ") >> rule1)
             >> qi::lit(" ") >> rule2
             >> qi::eoi,
         attr))
    std::cout<<"match"<<std::endl;
else
    std::cout<<"not match"<<std::endl;

We can of course use a look-ahead operator to check what's behind rule1,
but is there a more generic approach to implement regex operator '?' ?
Thanks!

_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
Reply | Threaded
Open this post in threaded view
|

Re: [spirit][qi] How to write a boost::spirit::qi parser to do what '?' does in regex?

sehe
On 14-10-16 16:25, Han Wang wrote:
> I try to use boost::spirit::qi parser to parse string2 but it couldn't
> match.

Answered on SO
https://stackoverflow.com/questions/40043636/how-to-write-a-boostspiritqi-parser-to-do-what-does-in-regex/40053714#40053714


_______________________________________________
Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost