#include <iostream>
#include <iterator>
#include <regex>
#include <string>
#include <vector>
using namespace std;
int main() {
string input {"Again this is the example in which shall be searched"};
string first {"again"};
string second {"be"};
vector<string> words;
words.push_back("this");
words.push_back("table");
const regex re("\\b" + first + accumulate(next(cbegin(words)), cend(words), "\\b(?:(\\b" + words.front(), [](const auto& lhs, const auto& rhs) { return lhs + "\\b|\\b" + rhs; }) + "\\b.*).*|.)*?" + second + "\\b", regex_constants::icase);
smatch sm;
if (regex_search(input, sm, re) && sm[1].length() == 0U) {
cout << "match\n";
} else {
cout << "not a match\n";
}
return 0;
}
ICAgICAgICAgICAgI2luY2x1ZGUgPGlvc3RyZWFtPgogICAgICAgICAgICAjaW5jbHVkZSA8aXRlcmF0b3I+CiAgICAgICAgICAgICNpbmNsdWRlIDxyZWdleD4KICAgICAgICAgICAgI2luY2x1ZGUgPHN0cmluZz4KICAgICAgICAgICAgI2luY2x1ZGUgPHZlY3Rvcj4KICAgICAKICAgICAgICAgICAgdXNpbmcgbmFtZXNwYWNlIHN0ZDsKICAgICAKICAgICAgICAgICAgaW50IG1haW4oKSB7CiAgICAgICAgICAgIAlzdHJpbmcgaW5wdXQgeyJBZ2FpbiB0aGlzIGlzIHRoZSBleGFtcGxlIGluIHdoaWNoIHNoYWxsIGJlIHNlYXJjaGVkIn07CiAgICAgCiAgICAgICAgICAgIAlzdHJpbmcgZmlyc3QgeyJhZ2FpbiJ9OwogICAgICAgICAgICAJc3RyaW5nIHNlY29uZCB7ImJlIn07CiAgICAgCiAgICAgICAgICAgIAl2ZWN0b3I8c3RyaW5nPiB3b3JkczsKICAgICAgICAgICAgCXdvcmRzLnB1c2hfYmFjaygidGhpcyIpOwogICAgICAgICAgICAJd29yZHMucHVzaF9iYWNrKCJ0YWJsZSIpOwogICAgIAogICAgIAogICAgICAgICAgICAJY29uc3QgcmVnZXggcmUoIlxcYiIgKyBmaXJzdCArIGFjY3VtdWxhdGUobmV4dChjYmVnaW4od29yZHMpKSwgY2VuZCh3b3JkcyksICJcXGIoPzooXFxiIiArIHdvcmRzLmZyb250KCksIFtdKGNvbnN0IGF1dG8mIGxocywgY29uc3QgYXV0byYgcmhzKSB7IHJldHVybiBsaHMgKyAiXFxifFxcYiIgKyByaHM7IH0pICsgIlxcYi4qKS4qfC4pKj8iICsgc2Vjb25kICsgIlxcYiIsIHJlZ2V4X2NvbnN0YW50czo6aWNhc2UpOwogICAgICAgICAgICAJc21hdGNoIHNtOwogICAgIAogICAgICAgICAgICAJaWYgKHJlZ2V4X3NlYXJjaChpbnB1dCwgc20sIHJlKSAmJiBzbVsxXS5sZW5ndGgoKSA9PSAwVSkgewogICAgICAgICAgICAJICAgIGNvdXQgPDwgIm1hdGNoXG4iOwogICAgICAgICAgICAJfSBlbHNlIHsKICAgICAgICAgICAgCSAgICBjb3V0IDw8ICJub3QgYSBtYXRjaFxuIjsKICAgICAgICAgICAgCX0KICAgICAgICAgICAgCXJldHVybiAwOwogICAgICAgICAgICB9