#include <regex>
#include <string>
#include <iostream>
using namespace std;

int main()
{
	string regx = R"(<\s*(\d+)\s*>((.|\n)*?)<\s*\1\s*>)";
	string input = "<1>test1<1><2>Tes \n t2<2>sfsaf<3><4>test4<4>";
	smatch matches;
	    while (regex_search(input, matches, regex(regx)))
	    {
	        cout<<matches[2]<<endl;
	        input = matches.suffix().str();
	    }
	return 0;
}