How to find all double quote substrings in a string with C++

1 Answer

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

int main() {
    std::string str = "This is a string with \"double-quoted substring1\", and \"double-quoted substring2\" inside.";

    // Regular expression to match substrings within double quotes
    std::regex pattern("\"([^\"]*)\"");
    std::smatch matches;

    std::vector<std::string> substrings;

    // Using std::regex_iterator to find all matches
    auto begin = std::sregex_iterator(str.begin(), str.end(), pattern);
    auto end = std::sregex_iterator();

    for (auto it = begin; it != end; ++it) {
        substrings.push_back((*it)[1]);
    }

    // Print extracted substrings
    for (const auto& substring : substrings) {
        std::cout << substring << std::endl;
    }
}

 
 
/*
run:
 
double-quoted substring1
double-quoted substring2
 
*/
   
 

 



answered May 13 by avibootz
...