2

ブースト正規表現に一致する文字列内のインデックスを見つけるにはどうすればよいですか?

4

2 に答える 2

7

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;
}
于 2008-10-24T16:26:50.327 に答える
6

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;
    }
}
于 2008-10-24T16:33:04.933 に答える