为什么 boost regex '.{2}' 不匹配 '??'
Why doesn't boost regex '.{2}' match '??'
如果数据流中有有趣的数据,我正在尝试匹配一些块。
应该有一个前导 <
然后是四个字母数字字符、两个校验和字符(或者 ??
如果没有指定 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 中删除
如果数据流中有有趣的数据,我正在尝试匹配一些块。
应该有一个前导 <
然后是四个字母数字字符、两个校验和字符(或者 ??
如果没有指定 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 中删除