2016-11-08 90 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

相關問題