How to Replace Text in a String Using Regex in C++? Last Updated : 08 Feb, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report Regular expressions or what we can call regex for short are sequences of symbols and characters that create a search pattern and help us to find specific patterns within a given text. In this article, we will learn how to replace text in a string using regex in C++. Example Input: str = "Hello, World! Hello, GFG!" ; text = "Hello"; replace= "Hi"; Output: Hi, World! Hi, GFG!Replace Regex Matches in C++ StringTo replace a text in a string using regex, we can use the function std::regex_replace(). It is defined inside the <regex> header so we need to include it in our program. Algorithm1. Define the main string, string to be replaced and replacement string.2. Create a regex object of the string you want to replace.3. Then pass the main string, regex object and replacement string to the regex_replace() function.4. The function regex_replace() returns a new string with the replacements applied. C++ Program to Replace Text in a String Using Regex C++ // C++ program to replace a pattern in a string using // regex. #include <iostream> #include <regex> #include <string> using namespace std; int main() { // target string string str = "Hello, World! Hello, GFG!"; // Pattern string string pattern = "Hello"; // regex object regex reg(pattern); // replacement string string replacement = "Hi"; // call regex_replace() and store the result string result = regex_replace(str, reg, replacement); // Output the result cout << "Original: " << str << endl; cout << "Modified: " << result << endl; return 0; } OutputOriginal: Hello, World! Hello, GFG! Modified: Hi, World! Hi, GFG! Time Complexity: O(N)Auxiliary Space: O(N) Comment More infoAdvertise with us Next Article How to Take Multiple Line String Input in C++? S susobhanakhuli19 Follow Improve Article Tags : C++ Programs C++ CPP-regex cpp-strings CPP Examples +1 More Practice Tags : CPPcpp-strings Similar Reads How to Reverse a String in C++? Reversing a string means replacing the first character with the last character, second character with the second last character and so on. In this article, we will learn how to reverse a string in C++.ExamplesInput: str = "Hello World"Output: dlroW olleHExplanation: The last character is replaced by 2 min read How to Reverse a String in Place in C++? In C++, reversing a string is a basic operation in programming that is required in various applications, from simple and complex algorithms. Reversing a string in place involves changing the characters of the string directly without using input-dependent additional storage. In this article, we learn 2 min read How to Take Multiple Line String Input in C++? In C++, taking string input is a common practice but the cin is only able to read the input text till whitespace. In this article, we will discuss how to read the multiple line of text input in C++. For Example, Input:Enter Your Text: This is amultiline text.Output:You Have Entered:This is amultilin 2 min read How to Use stringstream for Input With Spaces in C++? In C++, the std::stringstream class is a stream class to operate on strings and is very useful when we want to operate on a string as if it were a stream (like cin or cout). In this article, we will learn how to use string streams for input with spaces in C++.Example:Input: string = âHello, World!âO 2 min read regex_replace in C++ | Replace the match of a string using regex_replace std::regex_replace() is used to replace all matches in a string, Syntax: regex_replace(subject, regex_object, replace_text) Parameters: It accepts three parameters which are described below: Subject string as the first parameter. The regex object as the second parameter. The string with the replacem 3 min read How to Match a Pattern in a String in C++? In C++, strings are sequences of characters stored in a char array. Matching a pattern in a string involves searching for a specific sequence of characters (the pattern) within a given string. In this article, we will learn how to match a pattern in a string in C++. Example: Input:Text: "GeeksForGee 2 min read Like