为什么不提升正则表达式.{2}'匹配'??'

Jon*_*age 8 c++ boost trigraphs boost-regex

如果数据流中有趣的数据,我正在尝试匹配一些块.

应该有一个前导的<四个字母数字字符,两个校验和字符(或者??如果没有指定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
Run Code Online (Sandbox Code Playgroud)

我没有在文档中发现任何暗示应该是这种情况的东西(除了NULL和换行符应该匹配AIUI).

那么我错过了什么?

Dan*_*anh 10

因为??>三字形,它将被转换为},你的代码相当于:

// 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
Run Code Online (Sandbox Code Playgroud)

你可以改成这个:

std::string haystack = "Fli<data?" "?>bble";
Run Code Online (Sandbox Code Playgroud)

演示(注意:我使用std::regex哪个或多或少相同)

注意:从C++ 11中弃用了trigraph,将(可能)从C++中删除17