Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to match words in a string that are wrapped in curly brackets using RegEx with C++

3 Answers

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

int main() {
    std::string text = "This is a {string} with {words} in curly {brackets}";
    std::regex pattern("\\{([^}]+)\\}");
    std::smatch matches;

    std::string::const_iterator searchStart(text.cbegin());
    while (std::regex_search(searchStart, text.cend(), matches, pattern)) {
        std::cout << "Found: " << matches[1] << std::endl;
        searchStart = matches.suffix().first;
    }

    return 0;
}

 
  
  
/*
run:
 
Found: string
Found: words
Found: brackets
  
*/

 



answered Mar 17, 2025 by avibootz
0 votes
#include <iostream>
#include <regex>
#include <string>
#include <vector>

// Function to find and return all content within curly braces
std::vector<std::string> findCurlyBracesContent(const std::string& text) {
    std::vector<std::string> results;
    std::regex pattern("\\{([^}]+)\\}");
    std::smatch matches;

    std::string::const_iterator searchStart(text.cbegin());
    while (std::regex_search(searchStart, text.cend(), matches, pattern)) {
        results.push_back(matches[1]); // Add matched content to the results
        searchStart = matches.suffix().first; // Move the search to the next start point
    }
    
    return results;
}

int main() {
    std::string text = "This is a {string} with {words} in curly {brackets}";

    std::vector<std::string> results = findCurlyBracesContent(text);

    for (const auto& result : results) {
        std::cout << "Found: " << result << std::endl;
    }

    return 0;
}

  
/*
run:
 
Found: string
Found: words
Found: brackets
  
*/

 



answered Mar 17, 2025 by avibootz
0 votes
#include <iostream>
#include <string>
#include <regex>
#include <vector>

std::vector<std::string> extractWordsInBraces(const std::string& input) {
    std::vector<std::string> results;
    std::regex braceRegex("\\{(.*?)\\}"); // Regular expression to match {word}
    std::smatch match;

    std::string::const_iterator searchStart(input.cbegin());
    while (std::regex_search(searchStart, input.cend(), match, braceRegex)) {
        results.push_back(match[1]); // Extract the word inside braces
        searchStart = match.suffix().first; // Move searchStart past the current match
    }

    return results;
}

int main() {
    std::string text = "This is a string {word1} with multiple {word2} in braces {word3}.";
    std::vector<std::string> words = extractWordsInBraces(text);

    for (const auto& word : words) {
        std::cout << word << std::endl;
    }
}


/*
run:

word1
word2
word3

*/

 



answered Apr 9, 2025 by avibootz
...