Search code examples
c++boost-regex

How can I find the index in a string that matches a boost regex?


How can I find the index in a string that matches a boost regex?


Solution

  • If you use boost::regex_match it's the whole string that's matching.
    Maybe you mean to use regex_search:

    void index(boost::regex& re,const std::string& input){
        boost::match_results<std::string::const_iterator> what;
        boost::match_flag_type flags = boost::match_default;
        std::string::const_iterator s = input.begin();
        std::string::const_iterator e = input.end();
        while (boost::regex_search(s,e,what,re,flags)){
            std::cout << what.position() << std::endl;
            std::string::difference_type l = what.length();
            std::string::difference_type p = what.position();
            s += p + l;
        }
    }