3

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 0x04

In this case, 2 instances should be matched.

Many thanks!

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44
lzuwei
  • 31
  • 2

2 Answers2

0

Yes, boost::regex supports binary.

Jared Grubb
  • 1,139
  • 9
  • 17
0

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:

enter image description here

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

Shakiba Moshiri
  • 21,040
  • 2
  • 34
  • 44