Get last match with Boost::Regex

前端 未结 1 1797
鱼传尺愫
鱼传尺愫 2021-01-03 03:21

I have a regular expression in C++ with Boost which matches lines in multi-line strings. Regex search finds the first match, however I\'m interested in the last line which m

相关标签:
1条回答
  • 2021-01-03 03:42

    operator[] of match_results returns a sub_match. sub_match inherits std::pair of iterators. Its first and second members correspond to matched range. So, you can use its second for start point of new search. For example:

    string  input = "Data1 Data2 Data3";
    regex  re("(?<Group>Data.)");
    string::const_iterator  begin = input.begin(), end = input.end();
    smatch  results;
    while ( regex_search( begin, end, results, re ) ) {
      smatch::value_type  r = results["Group"];
      begin = r.second;
    }
    

    Hope this helps.

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