gpt4 book ai didi

c++ - 使用 boost::spirit phrase_parse 解析字符串

转载 作者:搜寻专家 更新时间:2023-10-31 01:52:47 25 4
gpt4 key购买 nike

我要解析字符串

    std::string entry = "127.0.0.1 - [16/Aug/2012:01:50:02 +0000] \"GET /check.htm HTTP/1.1\" 200 17 \"AgentName/0.1 libwww-perl/5.833\""

具有以下内容:

    ip_rule %= lexeme[(+char_("0-9."))[ref(ip) = _1]];
timestamp_rule %= lexeme[('[' >> +(char_ - ']') >> ']')[ref(timestamp) = _1]];
user_rule %= lexeme[(+char_)[ref(user) = _1]];
request_rule %= lexeme[('"' >> +(char_ - '"') >> '"')[ref(req) = _1]];
referer_rule %= lexeme[('"' >> +(char_ - '"') >> '"')[ref(referer) = _1]];

bool r = phrase_parse(first, last,
ip_rule >> user_rule >> timestamp_rule >> request_rule >> uint_[ref(status) = _1]
>> uint_[ref(transferred_bytes) = _1] >> referer_rule, space);

但是不匹配。如果我从字符串中删除“-”,当然还有规则“user_rule”,它就会匹配。能否请您指教如何将字符串与“-”匹配?

最佳答案

您的user_rule“吃掉”了文本的其余部分。像这样定义它:+~qi::char_("[")),这样它就会在 '[' 字符处停止。以下代码按预期工作:

#include <boost/spirit/include/qi.hpp> 
using namespace boost::spirit::qi;

int main()
{
std::string ip, user, timestamp, req, referer;
unsigned status, transferred_bytes;
std::string entry = "127.0.0.1 - [16/Aug/2012:01:50:02 +0000] \"GET /check.htm HTTP/1.1\" 200 17 \"AgentName/0.1 libwww-perl/5.833\"";
bool r = phrase_parse(entry.begin(), entry.end(),
lexeme[+char_("0-9.")] >>
+~char_("[") >>
lexeme[('[' >> +~char_("]") >> ']')] >>
lexeme[('"' >> +~char_("\"") >> '"')] >>
uint_ >>
uint_ >>
lexeme[('"' >> +~char_("\"") >> '"')], space, ip, user, timestamp, req, status, transferred_bytes, referer);

}

关于c++ - 使用 boost::spirit phrase_parse 解析字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12088192/

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