【问题标题】:Matching binary data using boost regex使用 boost 正则表达式匹配二进制数据
【发布时间】:2011-11-12 14:40:27
【问题描述】:

boost 正则表达式是否能够匹配给定二进制输入中的二进制数据?

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

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

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

非常感谢!

【问题讨论】:

    标签: c++ boost-regex


    【解决方案1】:

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

    【讨论】:

    【解决方案2】:

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

    regex boost 库比C++ 强大得多,正如您在屏幕截图中看到的那样:

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-07
      • 2017-01-25
      • 1970-01-01
      • 1970-01-01
      • 2017-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多