How to extract a substring between two tags using RegEx in C++

1 Answer

0 votes
#include <iostream>
#include <string>
#include <regex>

std::string extractContentBetweenTags(const std::string& str, const std::string& tagName) {
    // Build a regex pattern using the specified tag name
    std::string pattern = "<" + tagName + ">(.*?)</" + tagName + ">";
    std::regex regexPattern(pattern);
    std::smatch match;

    // Use regex to match the pattern
    if (std::regex_search(str, match, regexPattern)) {
        // Return the content inside the tags
        return match[1].str();
    }

    // Return an empty string if no match is found
    return "";
}

int main() {
    std::string str = "abcd <tag>efg hijk lmnop</tag> qrst uvwxyz";

    // Call the function to extract the substring
    std::string content = extractContentBetweenTags(str, "tag");

    if (!content.empty()) {
        std::cout << "Extracted content: " << content << std::endl;
    } else {
        std::cout << "No matching tags found." << std::endl;
    }

    return 0;
}


/*
run:

Extracted content: efg hijk lmnop

*/

 



answered Apr 2, 2025 by avibootz
...