2011-09-08 74 views
3

boost regex能夠匹配給定二進制輸入中的二進制數據嗎?使用boost正則表達式匹配二進制數據

例:
輸入以二進制形式:
0x01 0x02 0x03 0x04 0x05 0x01 0x02 0x03 0x04 0x08

二進制表達式匹配:
0x01 0x02 0x03 0x04

在這種情況下,2個實例亦宜。

非常感謝!

回答

0

是的,boost :: regex支持二進制。

+0

請看[ask]和[answer] –

0

你的問題對我來說還不夠乾淨。所以如果這個答案不是你在找什麼 ,只是告訴我我會刪除吧。

regex Boost庫是非常強大的比C++,你可以在截圖:

enter image description here

picture source

而且當C++可以做到這一點,當然的Boost也可以做到這一點。
std::regex::iterator

std::string binary("0x01 0x02 0x03 0x04 0x05 0x01 0x02 0x03 0x04 0x08"); 
std::basic_regex<char> regex("0x01 0x02 0x03 0x04"); 
// or 
// std::basic_regex<char> regex("0x01.+?4"); 
std::regex_iterator<std::string::iterator> last; 
std::regex_iterator<std::string::iterator> begin(binary.begin(), binary.end(), regex); 

while(begin != last){ 
    std::cout << begin->str() << '\n'; 
    ++begin; 
} 

輸出

0x01 0x02 0x03 0x04 
0x01 0x02 0x03 0x04 


std::regex_token::iterator

std::string binary("0x01 0x02 0x03 0x04 0x05 0x01 0x02 0x03 0x04 0x08"); 
std::basic_regex<char> regex(" 0x0[58] ?"); 
std::regex_token_iterator<std::string::iterator> last; 
std::regex_token_iterator<std::string::iterator> begin(binary.begin(), binary.end(), regex, -1); 

while(begin != last){ 
    std::cout << *begin << '\n'; 
    ++begin; 
} 

輸出
一樣


隨着升壓

std::string binary("0x01 0x02 0x03 0x04 0x05 0x01 0x02 0x03 0x04 0x08"); 
boost::basic_regex<char> regex(" 0x0[58] ?"); 

boost::regex_token_iterator<std::string::const_iterator> last; 
boost::regex_token_iterator<std::string::const_iterator> begin(binary.begin(), binary.end(), regex, -1); 

while(begin != last){ 
    std::cout << *begin << '\n'; 
    ++begin; 
} 

輸出
一樣

區別:的std :: string ::爲const_iterator,而不是std :: string :: iterator