regex_replace in C++ | Replace the match of a string using regex_replace Last Updated : 04 Sep, 2018 Summarize Comments Improve Suggest changes Share Like Article Like Report 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 replacement text as the third parameter. Return Value: Function returns a new string with the replacements applied. $& or $0 is used to insert the whole regex match. $1, $2, ... up to $9 is used to insert the text matched by the first nine capturing groups. $` (back-tick) is used to insert the string that is left of the match. $' (quote) is used to insert the string that is right of the match. If number of capturing group is less than the requested, then that will be replaced by nothing. Examples: Suppose a regex object re("(geeks)(.*)") is created and the subject string is: subject("its all about geeksforgeeks"), you want to replace the match by the content of any capturing group (eg $0, $1, ... upto 9). Example-1: Replace the match by the content of $1. Here match is "geeksforgeeks" that will be replaced by $1("geeks"). Hence, result "its all about geeks". Example-2: Replace the match by the content of $2. Here match is "geeksforgeeks" that will be replaced by $2("forgeeks"). Hence, result "its all about forgeeks". Below is the program to show the working of regex_replace. CPP // C++ program to show the working // of regex_replace #include <bits/stdc++.h> using namespace std; int main() { string subject("its all about geeksforgeeks"); string result1, result2, result3, result4; string result5; // regex object regex re("(geeks)(.*)"); // $2 contains, 2nd capturing group which is (.*) means // string after "geeks" which is "forgeeks". hence // the match(geeksforgeeks) will be replaced by "forgeeks". // so the result1 = "its all about forgeeks" result1 = regex_replace(subject, re, "$2"); // similarly $1 contains, 1 st capturing group which is // "geeks" so the match(geeksforgeeks) will be replaced // by "geeks".so the result2 = "its all about geeks". result2 = regex_replace(subject, re, "$1"); // $0 contains the whole match // so result3 will remain same. result3 = regex_replace(subject, re, "$0"); // $0 and $& contains the whole match // so result3 will remain same result4 = regex_replace(subject, re, "$&"); // Here number of capturing group // is 2 so anything above 2 // will be replaced by nothing. result5 = regex_replace(subject, re, "$6"); cout << result1 << endl << result2 << endl; cout << result3 << endl << result4 << endl << result5; return 0; } Output: its all about forgeeks its all about geeks its all about geeksforgeeks its all about geeksforgeeks its all about Comment More infoAdvertise with us Next Article regex_replace in C++ | Replace the match of a string using regex_replace V vivek kumar 9 Follow Improve Article Tags : C++ Programs C++ Practice Tags : CPP Similar Reads How to Replace Text in a String Using Regex in C++? 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, Worl 2 min read How to Find and Replace All Occurrences of a Substring in a C++ String? In C++, strings are sequences of characters that are used to represent textual data. In this article, we will learn how to find and replace all the occurrences of a particular substring in the given string. For Example, Input: str = "Lokesh is a good programmer, but Lokesh is still in the learning p 2 min read Modify array of strings by replacing characters repeating in the same or remaining strings Given an array of strings arr[] consisting of lowercase and uppercase characters only, the task is to modify the array by removing the characters from the strings which are repeating in the same string or any other string. Print the modified array. Examples: Input: arr[] = {"Geeks", "For", "Geeks"}O 6 min read C++ Program to Replace a Character in a String Given a string S, c1 and c2. Replace character c1 with c2 and c2 with c1. Examples: Input: grrksfoegrrks, c1 = e, c2 = r Output: geeksforgeeks Input: ratul, c1 = t, c2 = h Output: rahul Traverse through the string and check for the occurrences of c1 and c2. If c1 is found then replace it with c2 and 3 min read Extracting IP Address From a Given String Using Regular Expressions Prerequisites: Regular ExpressionsIP addressGiven a string str, the task is to extract all the IP addresses from the given string. Let us see how to extract IP addresses from a file. Input: str="The IPV4 address of Port A is: 257.120.223.13. And the IPV6 address is:fffe:3465:efab:23fe:2235:6565:aaab 4 min read Like