如何使用 regex_token_iterator<std::string::iterator> 通过迭代器本身获取原始字符串的子匹配位置?

Nin*_*ing 1 c++ regex

下面是在“这个主题有一个潜艇作为子序列”中找到“\b(sub)([^ ]*)”匹配的代码。但我也想通过 regex_token_iterator 本身知道这些子匹配在原始字符串中的位置。结果应该是 5、19、34。

// regex_token_iterator example
#include <iostream>
#include <string>
#include <regex>

int main ()
{
  std::string s ("this subject has a submarine as a subsequence");
  std::regex e ("\\b(sub)([^ ]*)");   // matches words beginning by "sub"

  // default constructor = end-of-sequence:
  std::regex_token_iterator<std::string::iterator> rend;

  std::cout << "entire matches:"; 
  std::regex_token_iterator<std::string::iterator> a ( s.begin(), s.end(), e );
  while (a!=rend) std::cout << " [" << *a++ << "]";
  std::cout << std::endl;

  return 0;
}
Run Code Online (Sandbox Code Playgroud)

输出:
整个 amtches:[subject] [submarine] [subsequence]

Hug*_*l31 5

*a在字符串s 上返回一对两个迭代器。你可以试试这个:

std::cout << " [" << *a++ << ' ' << a->first - s.begin() << "]";
Run Code Online (Sandbox Code Playgroud)

或这个

std::cout << " [" << *a++ << ' ' <<  std::distance(s.begin(), a->first) << "]";
Run Code Online (Sandbox Code Playgroud)