Как я могу найти индекс в строке, которая соответствует регулярному выражению boost?

StackOverflow https://stackoverflow.com/questions/234027

  •  04-07-2019
  •  | 
  •  

Вопрос

Как найти индекс в строке, соответствующей регулярному выражению в надстройке?

Это было полезно?

Решение

Если вы используете boost :: regex_match, то соответствует вся строка.
Может быть, вы хотите использовать 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;
    }
}

Другие советы

Используйте функцию-член position в 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;
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top