Boost C++ regex - how to get multiple matches

后端 未结 1 626
名媛妹妹
名媛妹妹 2020-12-03 21:01

If I have a simple regex pattern like \"ab.\" and I have a string that has multiple matches like \"abc abd\". If I do the following...

boost::match_flag         


        
相关标签:
1条回答
  • 2020-12-03 22:01

    You can use the boost::sregex_token_iterator like in this short example:

    #include <boost/regex.hpp>
    #include <iostream>
    #include <string>
    
    int main() {
        std::string text("abc abd");
        boost::regex regex("ab.");
    
        boost::sregex_token_iterator iter(text.begin(), text.end(), regex, 0);
        boost::sregex_token_iterator end;
    
        for( ; iter != end; ++iter ) {
            std::cout<<*iter<<'\n';
        }
    
        return 0;
    }
    

    The output from this program is:

    abc
    abd
    
    0 讨论(0)
提交回复
热议问题