使用 boost::spirit 来匹配单词

Using boost::spirit to match words

我想创建一个解析器来匹配字符串中的两个字母数字单词,例如:

message1 message2

然后将其保存到两个std::string类型的变量中。

我读过 this previous answer,它似乎适用于无数次重复,它使用以下解析器:

+qi::alnum % +qi::space

但是当我尝试这样做时:

 bool const result = qi::phrase_parse(
            input.begin(), input.end(),
            +qi::alnum >> +qi::alnum,
            +qi::space,
            words
    );

words 向量包含不同字符串中的每个字母:

't'
'h'
'i'
's'
'i'
's'

这非常违反直觉,我不确定为什么会这样。有人可以解释一下吗?

此外,我可以填充两个预定义字符串而不是 std::vector 吗?

最后说明: 我想避免使用 using 语句,因为我希望每个命名空间都明确定义以帮助我了解精神如何运作。

是的,但是船长会在您操作之前忽略空格。

使用lexeme控制船长:

bool const result = qi::phrase_parse(
        input.begin(), input.end(),
        qi::lexeme [+qi::alnum] >> qi::lexeme [+qi::alnum],
        qi::space,
        words
);

注意船长应该是 qi::space 而不是 +qi::space

另见 Boost spirit skipper issues