gpt4 book ai didi

c++ - 路径验证的 spirit 语法

转载 作者:行者123 更新时间:2023-11-30 03:43:06 25 4
gpt4 key购买 nike

我正在尝试使用 boost spirit 编写一个简单的语法来验证字符串是否为有效目录。我正在使用这些教程,因为这是我尝试的第一个语法: http://www.boost.org/doc/libs/1_36_0/libs/spirit/doc/html/spirit/qi_and_karma.html http://www.boost.org/doc/libs/1_48_0/libs/spirit/doc/html/spirit/qi/reference/directive/lexeme.html http://www.boost.org/doc/libs/1_44_0/libs/spirit/doc/html/spirit/qi/tutorials/employee___parsing_into_structs.html

目前,我想到的是:

// I want these to be valid matches
std::string valid1 = "./";
// This string could be any number of sub dirs i.e. /home/user/test/ is valid
std::string valid2 = "/home/user/";

using namespace boost::spirit::qi;
bool match = phrase_parse(valid1.begin(), valid1.end(), lexeme[
((char_('.') | char_('/')) >> +char_ >> char_('/')],
ascii::space);
if (match)
{
std::cout << "Match!" << std::endl;
}

但是,这不匹配任何内容。关于原因,我有一些想法;然而,在做了一些研究之后,我还没有找到答案。例如,我假设 +char_ 可能会消耗所有字符?那么我怎样才能知道一些字符序列是否都以/结尾呢?

基本上我编写上述代码的想法是我希望目录以 .和/有效,最后一个字符必须是/。有人可以帮助我学习语法或指出与我想做的事情更相似的例子吗?这纯粹是学习如何使用 spirit 的一种锻炼。

编辑所以我有解析器来匹配使用:

bool match = phrase_parse(valid1.begin(), valid1.end(), lexeme[
((char_('.') | char_('/')) >> *(+char_ >> char_('/'))],
ascii::space);
if (match)
{
std::cout << "Match!" << std::endl;
}

不确定这是否正确?或者如果它由于其他原因匹配...这里也应该使用 ascii::space 吗?我在一个教程中读到它是为了使空间不可知,即 a b 等同于 ab。我不想在路径名中使用哪个?如果它不是正确的使用方式,那会是什么?

SSCCE:

#include <string>
#include <iostream>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/qi_char.hpp>
#include <boost/spirit/include/qi_eoi.hpp>

int main()
{
namespace qi = boost::spirit::qi;
std::string valid1 = "./";
std::string valid2 = "/home/blah/";
bool match = qi::parse(valid2.begin(), valid2.end(), &((qi::lit("./")|'/') >> (+~qi::char_('/') % '/') >> qi::eoi));

if (match)
{
std::cout << "Match" << std::endl;
}
}

最佳答案

如果您不想忽略空格差异(您不应该这样做),请使用 parse 而不是 phrase_parselexeme 的使用再次抑制了 skipper (所以你只是剥离了前导/尾随空间)。另请参阅 stackoverflow.com/questions/17072987/boost-spirit-skipper-issues/17073965#17073965

使用 char_("ab") 代替 char_('a')|char_('b')

*char_ 匹配所有内容。您可能指的是 *~char_('/')

我建议像这样

 bool ok = qi::parse(b, f, &(lit("./")|'/') >> (*~char_('/') % '/'));

这不会公开匹配的输入。在它周围添加 raw[] 以实现这一点。

添加 > qi::eoi 断言所有输入都被消耗了。

关于c++ - 路径验证的 spirit 语法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36334128/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com