regex_replace in C++ | Replace the match of a string using regex_replace

04 Sep, 2018
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.
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.
$& 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.
// 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;}
its all about forgeeks
its all about geeks
its all about geeksforgeeks
its all about geeksforgeeks
its all about

CPP-Library
CPP-regex
C++
CPP
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Sorting a vector in C++
Polymorphism in C++
Friend class and function in C++
Pair in C++ Standard Template Library (STL)
std::string class in C++
Queue in C++ Standard Template Library (STL)
Unordered Sets in C++ Standard Template Library
std::find in C++
List in C++ Standard Template Library (STL)
Inline Functions in C++