开发者

Matching binary data using boost regex

开发者 https://www.devze.com 2023-04-03 04:29 出处:网络
Is boost regex able to match binary data in a given binary input? Ex.: Input in binary form: 0x01 0x02 0x03 0x04 0x05 0x01 0x02 0x03 0x04 0x08

Is boost regex able to match binary data in a given binary input?

Ex.:

Input in binary form:

0x01 0x02 0x03 0x04 0x05 0x01 0x02 0x03 0x04 0x08

Binary expression to match:

0x01 0x02 0x03 0x开发者_StackOverflow中文版04

In this case, 2 instances should be matched.

Many thanks!


Yes, boost::regex supports binary.


Your question is not clean enough to me. So If this answer is not what you looking for, just tell me I will delete it.

The regex boost library is much powerful than C++ as you can in the screenshot:

Matching binary data using boost regex

picture source

Also when the C++ can do it, of course Boost can do it, as well.
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;
}  

output

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

or
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;
}

output
as the same


With boost

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;
}  

output
as the same

difference: std::string::const_iterator, instead of std::string::iterator

0

精彩评论

暂无评论...
验证码 换一张
取 消