Online Book Reader

Home Category

Professional C__ - Marc Gregoire [263]

By Root 1062 0
SA>& fmt,

regex_constants::match_flag_type flags =

regex_constants::match_default);

template class traits, class charT>

OutputIterator

regex_replace(OutputIterator out,

BidirectionalIterator first,

BidirectionalIterator last,

const basic_regex& e,

const charT* fmt,

regex_constants::match_flag_type flags =

regex_constants::match_default);

template

basic_string

regex_replace(const basic_string& s,

const basic_regex& e,

const basic_string& fmt,

regex_constants::match_flag_type flags =

regex_constants::match_default);

template

basic_string

regex_replace(const basic_string& s,

const basic_regex& e,

const charT* fmt,

regex_constants::match_flag_type flags =

regex_constants::match_default);

template

basic_string

regex_replace(const charT* s,

const basic_regex& e,

const basic_string& fmt,

regex_constants::match_flag_type flags =

regex_constants::match_default);

template

basic_string

regex_replace(const charT* s,

const basic_regex& e,

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

Header

Some text

and the regular expression

(.*)

(.*)

. The following table shows the different escape sequences and with what they will be replaced with:

ESCAPE SEQUENCE REPLACED WITH

$1 Header

$2 Some text

$&

Header

Some text

$'

$'

The following code demonstrates the use of regex_replace():

const string str("

Header

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: '

Header

Some text

'

New string : 'H1=Header and P=Some text'

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("

Header

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: '

Header

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. Note

Return Main Page Previous Page Next Page

®Online Book Reader