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

早过忘川 提交于 2019-12-01 11:03:00

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

Use the position member function of the match_results:

int find_match_offset(std::string const& string_to_search,
                      boost::regex const& expression)
{
    boost::smatch results;
    if(boost::regex_match(string_to_search,results,expression))
    {
        return results.position()
    }
    return -1;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!