2016-11-08 92 views
8

我想匹配一些数据流中有趣的数据块。为什么不提高正则表达式'。{2}'匹配'??'

应该有一个领先的<,然后是四个字母数字字符,两个校验和(或??,如果没有指定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)。

那么我错过了什么?

+1

您使用的编译器是什么? Mine(gcc)给出了一个明确的警告,说明“trigraph ??>转换为}”。 – SingerOfTheFall

+0

我在2008工具链中使用visual studio 2013。 –

回答

10

因为??>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大致相同)

说明: trigraph从C++ 11弃用,将(可能)从C++中删除17

+0

你明白了。非常有趣 - 我以前没有听说过三联草图! –

+0

已被删除(或已弃用?)最新标准 – sehe

+1

@sehe弃用C++ 11,将被C++ 17删除 – Danh

相关问题