簡體   English   中英

為什么不提升正則表達式。{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

我沒有在文檔中發現任何暗示應該是這種情況的東西(除了NULL和換行符應該匹配AIUI)。

那么我錯過了什么?

因為??>是一個三字形 ,它將被轉換為} ,你的代碼相當於:

// 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";

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

注意:從C ++ 11中棄用了trigraph,將(可能)從C ++中刪除17

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM