How to replace the characters !@#$%^*_+\= in a string using RegEx with C++

1 Answer

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

int main() {
    std::string input = "The!quick@brown#fox$jumps%^over*_the+\\lazy=dog.";
    std::regex pattern("[!@#$%^*_+=\\\\]"); 
    std::string replacement = " ";

    // Perform regex replacement
    std::string result = std::regex_replace(input, pattern, replacement);

    std::cout << "Original: " << input << std::endl;
    std::cout << "Modified: " << result << std::endl;
}


/*
run:

Original: The!quick@brown#fox$jumps%^over*_the+\lazy=dog.
Modified: The quick brown fox jumps  over  the  lazy dog.

*/

 



answered Jun 11, 2025 by avibootz
...