Search code examples
c++parsingboost-spiritboost-spirit-qi

Parse string containing literal with Boost Spirit Qi


I would love to parse a string like this:

<stuff I don't care> <literal value> <more stuff I don't care>

with boost::spirit::qi. Let's assume that <literal value> is e.g. ABC, then I would like the parser to accept:

Some text ABC more text

but reject:

Some text ACB more text

Unfortunately,

*char_ >> lit("ABC") >> *char_

does not work due to qi's greediness. Is there an easy way to write this parser?


Solution

  • Use

    *(char_ - lit("ABC")) >> lit("ABC") >> *char_;
    

    instead to prevent char_ from consuming "ABC".