#include <iostream>
#include <regex>
#include <string>
using namespace std;
int main() {
std::regex r("(?:^|\n)(\\d+)(?=$|\n)");
std::string s = "1\n2\n3";
for(std::sregex_iterator i = std::sregex_iterator(s.begin(), s.end(), r);
i != std::sregex_iterator();
++i)
{
std::smatch m = *i;
std::cout << " Capture: " << m[1].str() << " at Position " << m.position(1) << '\n';
}
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8cmVnZXg+CiNpbmNsdWRlIDxzdHJpbmc+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgppbnQgbWFpbigpIHsKCXN0ZDo6cmVnZXggcigiKD86XnxcbikoXFxkKykoPz0kfFxuKSIpOwogICAgc3RkOjpzdHJpbmcgcyA9ICIxXG4yXG4zIjsKICAgIGZvcihzdGQ6OnNyZWdleF9pdGVyYXRvciBpID0gc3RkOjpzcmVnZXhfaXRlcmF0b3Iocy5iZWdpbigpLCBzLmVuZCgpLCByKTsKICAgICAgICAgICAgICAgICAgICAgICAgICAgICBpICE9IHN0ZDo6c3JlZ2V4X2l0ZXJhdG9yKCk7CiAgICAgICAgICAgICAgICAgICAgICAgICAgICAgKytpKQogICAgewogICAgICAgIHN0ZDo6c21hdGNoIG0gPSAqaTsKICAgICAgICBzdGQ6OmNvdXQgPDwgIiAgICBDYXB0dXJlOiAiIDw8IG1bMV0uc3RyKCkgPDwgIiBhdCBQb3NpdGlvbiAiIDw8IG0ucG9zaXRpb24oMSkgPDwgJ1xuJzsKICAgIH0KCXJldHVybiAwOwp9