I have the following std::string
:
<lots of text not including "label A" or "label B">
label A: 34
<lots of text not including "label A" or "label B">
label B: 45
<lots of text not including "label A" or "label B">
...
I want extract single integral numbers following all occurrences of label A
or label B
and place them in corresponding vector<int> a, b
. A simple, but not elegant way of doing it is using find("label A")
and find("label B")
and parsing whichever is first. Is there a succinct way of expressing it using Spirit? How do you skip everything but label A
or label B
?
You can just
Example: Live On Coliru
There's also the
seek[]
directive in the repository. The following is equivalent to the above:Here's a sample that parses your original sample:
This will parse into
std::vector<std::pair<char, int> >
without anything else.On Coliru Too:
Notes:
seek
does expose the attribute matched, which is pretty powerful:will 'eat' the label, but expose the label letter (
'A'
,'B'
,'C'
, or'D'
) as the attribute.Performance when skipping can be pretty surprising, read the warning in the documentation http://www.boost.org/doc/libs/1_55_0/libs/spirit/repository/doc/html/spirit_repository/qi_components/directives/seek.html
Output is