C++提取两个字符串之间的数据

C++ to extract data between two strings

我正在寻找一个 c++ 代码,它可以从两个字符串之间的文件 example.txt 中提取一些特定内容并忽略其余内容。例如文件 example.txt 有以下行

xyz
abc
['Content','en']],
<html>hi this is a line <br></html>
',true], 
suzi 20

我想提取 ['Content','en']],',true],[=20 之间的代码=] 表示

<html>hi this is a line <br></html>

请注意,我不擅长编程和使用 dev++ 编译器

最简单的思路就是将文件读入字符串,然后提取内容:

#include <string>
#include <sstream>

std::string extract(std::string const& tag_begin, std::string const& tag_end, std::istream& input)
{
    // file stream -> in memory string
    std::string filedata((std::istreambuf_iterator<char>(input)), std::istreambuf_iterator<char>());

    // find content start and stop
    auto content_start = filedata.find(tag_begin);
    if (content_start == std::string::npos) {
        return ""; // error handling
    }
    content_start += tag_begin.size();
    auto content_end   = filedata.find(tag_end, content_start);
    auto content_size  = content_end - content_start;

    // extract (copy) content to other string
    auto content = filedata.substr(content_start, content_size);
    return content;
}

live demo

然后,您需要根据您的需要调整此通用解决方案。