如果数据流中的有趣数据,我正在尝试匹配一些 block 。
应该有一个前导<
然后是四个字母数字字符、两个校验和字符(或 ??
如果未指定 shecksum)和尾随 >
.
如果最后两个字符是字母数字,则以下代码按预期工作。如果他们是 ??
尽管它失败了。
// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data??>bble";
// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;
// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false
我在 the documentation 中没有发现任何东西这表明应该是这种情况(除了 NULL 和换行符之外的所有内容都应该匹配 AIUI)。
那么我错过了什么?
最佳答案
因为 ??>
是一个 trigraph ,它会被转换成,你的代码相当于:
// Set up a pre-populated data buffer as an example
std::string haystack = "Fli<data}bble";
// Set up the regex
static const boost::regex e("<\\w{4}.{2}>");
std::string::const_iterator start, end;
start = haystack.begin();
end = haystack.end();
boost::match_flag_type flags = boost::match_default;
// Try and find something of interest in the buffer
boost::match_results<std::string::const_iterator> what;
bool succeeded = regex_search(start, end, what, e, flags); // <-- returns false
你可以改成这样:
std::string haystack = "Fli<data?" "?>bble";
Demo (注意:我使用的 std::regex
大致相同)
注意:三字母表已从 C++11 中弃用,将(可能)从 C++17 中删除
关于c++ - 为什么 boost 正则表达式 '.{2}' 不匹配 '??',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40483766/