使用boost regex partial_match避免使用$ match结束缓冲区



我正在用cregex_iterator搜索文件。我在我的匹配标志中设置了boost::regex_constants::match_partial和boost::regex_constants::match_not_eob,并且在正则表达式的末尾设置了$。问题是$似乎与缓冲区的末尾匹配。因为我正在寻找部分匹配在我的缓冲区的末尾,以方便搜索一个文件,这不起作用。有没有办法让$不匹配缓冲区的末尾?下面的简化测试示例…

#include <iostream>
#include <boost/regex.hpp>
const char *buf =
R"V0G0N([2014-04-12 13:01:13.414+0100:0x00000134]: Arbitrary non matching line
[2014-04-12 13:01:14.570+0100:0x00000134]:DBLog: 'Incomplete)V0G0N";
const char *szRegex = "^\[(.{23})(Z|[+|-][0-9]{2})[0-9x:]{11,13}\]:DBLog: (.+)$";
int _tmain(int argc, char* argv[])
{
    char c;
    boost::regex::flag_type regex_flags = boost::regex_constants::normal | boost::regex_constants::perl;
    boost::regex_constants::match_flag_type match_flags =
        boost::regex_constants::match_default | boost::regex_constants::match_not_dot_newline | boost::regex_constants::format_perl |
        boost::regex_constants::match_partial | boost::regex_constants::match_not_eob;
    boost::regex pattern(szRegex, regex_flags);
    boost::cregex_iterator a(
        buf,
        buf + strlen(buf),
        pattern,
        match_flags);
    boost::cregex_iterator end;
    while (a != end)
    {
        if ((*a)[0].matched == false)
        {
            // Partial match, save position and break:
            std::cout << "Partial Match: " << std::string((*a)[0].first, (*a)[0].second) << std::endl;
            break;
        }
        else
        {
            std::cout << "Complete Match: " << std::string((*a)[0].first, (*a)[0].second) << std::endl;
        }
        // move to next match:
        ++a;
    }
    std::cout << "done!" << std::endl;
    std::cin >> c;
    return 0;
}

结果……

Complete Match: [2014-04-12 13:01:14.570+0100:0x00000134]:DBLog: 'Incomplete

我需要的是将其作为部分匹配处理

http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/ref/match_flag_type.html

在这里我终于弄明白了

match_not_eob

不适用于'$'。我需要使用

match_not_eol

最新更新