我正在使用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
我需要的是将其视为部分匹配。
答案 0 :(得分:0)
http://www.boost.org/doc/libs/1_55_0/libs/regex/doc/html/boost_regex/ref/match_flag_type.html
在这里我终于想通了
match_not_eob
不适用于&#39; $&#39;。我需要改为使用
match_not_eol