Может быть хорошим примером для std :: regex, который является частью C ++ 11.
#include <iostream>
#include <string>
#include <regex>
int main()
{
using namespace std::string_literals;
auto start = "\\[STRING1\\]"s;
auto end = "\\[STRING2\\]"s;
std::regex base_regex(start + "(.*)" + end);
auto example = "832h0ufhu0sdf4[STRING1]I need this text here[STRING2]afyh0fhdfosdfndsf"s;
std::smatch base_match;
std::string matched;
if (std::regex_search(example, base_match, base_regex)) {
// The first sub_match is the whole string; the next
// sub_match is the first parenthesized expression.
if (base_match.size() == 2) {
matched = base_match[1].str();
}
}
std::cout << "example: \""<<example << "\"\n";
std::cout << "matched: \""<<matched << "\"\n";
}
Печать:
example: "832h0ufhu0sdf4[STRING1]I need this text here[STRING2]afyh0fhdfosdfndsf"
matched: "I need this text here"
Я создал программу, которая создает две строки, начало и конец, которые служат моими началом и концом совпадений. Затем я использую строку регулярного выражения, которая будет искать их и сопоставлять с чем-либо промежуточным (включая ничего). Затем я использую regex_match, чтобы найти совпадающую часть выражения, и устанавливаю matched в качестве совпадающей строки.
Для получения дополнительной информации см. http://en.cppreference.com/w/cpp/regex и http://en.cppreference.com/w/cpp/regex/regex_search