Boost logo

Boost :

Subject: [boost] [spirit][qi] How to write a boost::spirit::qi parser to do what '?' does in regex?
From: Han Wang (wanghan02_at_[hidden])
Date: 2016-10-14 10:25:09

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

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

    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
qi::rule<std::string::const_iterator, std::string()> rule1 =
qi::rule<std::string::const_iterator, std::string()> rule2 = qi::digit >>
std::vector<std::string> attr;
auto it_begin = string2.begin();
auto it_end = string2.end();
if (qi::parse(
>> -(qi::lit(" ") >> rule1)
>> qi::lit(" ") >> rule2
>> qi::eoi,
    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 '?' ?

Boost list run by bdawes at, gregod at, cpdaniel at, john at