3

boost regex 是否能够匹配给定二进制输入中的二进制数据?

例如:
以二进制形式输入:
0x01 0x02 0x03 0x04 0x05 0x01 0x02 0x03 0x04 0x08

要匹配的二进制表达式:
0x01 0x02 0x03 0x04

在这种情况下,应匹配 2 个实例。

非常感谢!

4

2 回答 2

0

你的问题对我来说不够干净。所以如果这个答案不是你想要的,告诉我我会删除它。

boost 库比屏幕截图中的 强大regex得多:C++

在此处输入图像描述

图片来源

同样,当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

于 2017-02-10T22:23:59.560 回答
0

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

于 2017-02-10T18:45:00.983 回答