How to extract a substring between two tags [start] [end] in a string with C++

4 Answers

0 votes
// Most direct and efficient solution Using std::string::find 

#include <iostream>
#include <string>

// Extract substring between two tags using std::string::find
std::string extractBetween(const std::string& text,
                           const std::string& startTag,
                           const std::string& endTag)
{
    // Find the start tag
    size_t startPos = text.find(startTag);
    if (startPos == std::string::npos)
        return ""; // start tag not found

    // Move index to the end of the start tag
    startPos += startTag.length();

    // Find the end tag after the start tag
    size_t endPos = text.find(endTag, startPos);
    if (endPos == std::string::npos)
        return ""; // end tag not found

    // Extract substring between the tags
    return text.substr(startPos, endPos - startPos);
}

int main() {
    std::string s = "What do you [start]think about this[end] idea?";
    
    std::cout << extractBetween(s, "[start]", "[end]") << "\n";
}


/*
run:

think about this

*/

 



answered 20 hours ago by avibootz
0 votes
// Using std::regex

#include <iostream>
#include <string>
#include <regex>

// Extract substring using regex
std::string extractRegex(const std::string& text) {
    std::regex pattern("\\[start\\](.*?)\\[end\\]");
    std::smatch match;

    if (std::regex_search(text, match, pattern))
        return match[1]; // captured group

    return "";
}

int main() {
    std::string s = "What do you [start]think about this[end] idea?";
    
    std::cout << extractRegex(s) << "\n";
}



/*
run:

think about this

*/

 



answered 20 hours ago by avibootz
0 votes
// Manual Parsing (Character-by-Character)

#include <iostream>
#include <string>

// Manual parsing without find() or regex
std::string extractManual(const std::string& text) {
    const std::string startTag = "[start]";
    const std::string endTag   = "[end]";

    int state = 0; // 0 = searching start, 1 = reading content
    std::string buffer, result;

    for (size_t i = 0; i < text.size(); i++) {
        // Check for start tag
        if (state == 0 && text.compare(i, startTag.size(), startTag) == 0) {
            state = 1;
            i += startTag.size() - 1;
            continue;
        }

        // Check for end tag
        if (state == 1 && text.compare(i, endTag.size(), endTag) == 0) {
            return result;
        }

        // Collect characters
        if (state == 1)
            result += text[i];
    }

    return ""; // no valid match
}

int main() {
    std::string s = "What do you [start]think about this[end] idea?";
    
    std::cout << extractManual(s) << "\n";
}



/*
run:

think about this

*/

 



answered 20 hours ago by avibootz
0 votes
// Using std::stringstream

#include <iostream>
#include <sstream>
#include <string>

// Extract substring between two tags using a "streaming" style,
// but actually relying on find() for correctness.
std::string extractStream(const std::string& text,
                          const std::string& startTag,
                          const std::string& endTag) {
    // Use a stringstream only as a wrapper around the text
    std::stringstream ss(text);
    std::string buffer = ss.str(); // get the full string back

    // Find the start tag
    size_t startPos = buffer.find(startTag);
    if (startPos == std::string::npos)
        return ""; // start tag not found

    // Move to the end of the start tag
    startPos += startTag.length();

    // Find the end tag after the start tag
    size_t endPos = buffer.find(endTag, startPos);
    if (endPos == std::string::npos)
        return ""; // end tag not found

    // Extract the substring between the tags
    return buffer.substr(startPos, endPos - startPos);
}

int main() {
    std::string s = "What do you [start]think about this[end] idea?";
    
    std::cout << extractStream(s, "[start]", "[end]") << "\n";
}



/*
run:

think about this

*/

 



answered 20 hours ago by avibootz
...