在 Boost.Spirit 中,可以使用 qi::litqi::lexeme 来表达字符串。

  1. 使用 qi::lit 表达字符串时,字符串必须与输入完全匹配。例如,要匹配字符串 "hello",可以使用以下代码:
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;

std::string input = "hello";
std::string str;
bool success = qi::parse(input.begin(), input.end(), qi::lit("hello"), str);
  1. 使用 qi::lexeme 表达字符串时,字符串可以包含空白字符,并且不需要与输入完全匹配。例如,要匹配字符串 "hello world",可以使用以下代码:
#include <boost/spirit/include/qi.hpp>
namespace qi = boost::spirit::qi;

std::string input = "hello world";
std::string str;
bool success = qi::parse(input.begin(), input.end(), qi::lexeme["hello" >> *qi::char_], str);

在上述代码中,qi::lexeme 表示要匹配的字符串可以包含空白字符,*qi::char_ 表示匹配任意字符(包括空白字符)。

无论是使用 qi::lit 还是 qi::lexeme,都需要包含 <boost/spirit/include/qi.hpp> 头文件,并使用 namespace qi = boost::spirit::qi; 来定义命名空间别名。

boostspirit 中如何表达字符串

原文地址: http://www.cveoy.top/t/topic/iYNv 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录