Professional C__ - Marc Gregoire [263]
regex_constants::match_flag_type flags =
regex_constants::match_default);
template OutputIterator regex_replace(OutputIterator out, BidirectionalIterator first, BidirectionalIterator last, const basic_regex const charT* fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template basic_string regex_replace(const basic_string const basic_regex const basic_string regex_constants::match_flag_type flags = regex_constants::match_default); template basic_string regex_replace(const basic_string const basic_regex const charT* fmt, regex_constants::match_flag_type flags = regex_constants::match_default); template basic_string regex_replace(const charT* s, const basic_regex const basic_string regex_constants::match_flag_type flags = regex_constants::match_default); template basic_string regex_replace(const charT* s, const basic_regex const charT* fmt, regex_constants::match_flag_type flags = regex_constants::match_default); regex_replace() Examples As a first example, take the source HTML string Some text (.*) ESCAPE SEQUENCE REPLACED WITH $1 Header $2 Some text $& Some text $' $' The following code demonstrates the use of regex_replace(): const string str(" Some text regex r(" (.*) const string format("H1=$1 and P=$2"); string result = regex_replace(str, r, format); cout << "Original string: '" << str << "'" << endl; cout << "New string : '" << result << "'" << endl; Code snippet from RegularExpressions\regex_replace_1.cpp The output of this program is as follows: Original string: ' Some text New string : ' The regex_replace() algorithm accepts a number of flags that can be used to manipulate how it is working. The most important flags are given in the following table: FLAG DESCRIPTION format_default The default is to replace all occurrences of the pattern, and to also copy everything that does not match the pattern to the result string. format_no_copy Replace all occurrences of the pattern, but do not copy anything that does not match the pattern to the result string. format_first_only Replace only the first occurrence of the pattern. The following example modifies the previous code to use the format_no_copy flag: const string str(" Some text regex r(" (.*) const string format("H1=$1 and P=$2"); string result = regex_replace(str, r, format, regex_constants::format_no_copy); cout << "Original string: '" << str << "'" << endl; cout << "New string : '" << result << "'" << endl; Code snippet from RegularExpressions\regex_replace_2.cpp The output is as follows. Compare this with the output of the previous version. Original string: ' Some text New string : 'H1=Header and P=Some text' Another example is to get an input string and replace each word boundary with a newline so that the target string contains only one word per line. The following example demonstrates this without using any loops to process a given string. The code first creates a regular expression that matches individual words. When a match is found it is replaced by $1\n where $1 will be replaced with the matched word. NoteHeader
(.*)
Header
Header
(.*)
Header
Header
(.*)
Header