Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/xpressive/doc/substitutions.qbk @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 3.0 KB
Line 
1[section String Substitutions]
2
3Regular expressions are not only good for searching text; they're good at ['manipulating] it. And one of the most
4common text manipulation tasks is search-and-replace. xpressive provides the _regex_replace_ algorithm for
5searching and replacing.
6
7[h2 regex_replace()]
8
9Performing search-and-replace using _regex_replace_ is simple. All you need is an input sequence, a regex object,
10and a format string. There are two versions of the _regex_replace_ algorithm. The first accepts the input
11sequence as `std::basic_string<>` and returns the result in a new `std::basic_string<>`. The second accepts
12the input sequence as a pair of iterators, and writes the result into an output iterator. Below are examples of
13each.
14
15    std::string input("This is his face");
16    sregex re = as_xpr("his");                // find all occurrences of "his" ...
17    std::string format("her");                // ... and replace them with "her"
18
19    // use the version of regex_replace() that operates on strings
20    std::string output = regex_replace( input, re, format );
21    std::cout << output << '\n';
22
23    // use the version of regex_replace() that operates on iterators
24    std::ostream_iterator< char > out_iter( std::cout );
25    regex_replace( out_iter, input.begin(), input.end(), re, format );
26
27The above program prints out the following:
28
29[pre
30Ther is her face
31Ther is her face
32]
33
34Notice that ['all] the occurrences of `"his"` have been replaced with `"her"`.
35
36Click [link boost_xpressive.user_s_guide.examples.replace_all_sub_strings_that_match_a_regex here] to see a complete
37example program that shows how to use _regex_replace_. And check the _regex_replace_ reference
38to see a complete list of the available overloads.
39
40[h2 The Format String]
41
42As with Perl, you can refer to sub-matches in the format string. The table below shows the escape sequences
43xpressive recognizes in the format string.
44
45[table Format Escape Sequences
46    [[Escape Sequence]      [Meaning]]
47    [[[^$1]]                [the first sub-match]]
48    [[[^$2]]                [the second sub-match (etc.)]]
49    [[[^$&]]                [the full match]]
50    [[[^$\`]]               [the match prefix]]
51    [[[^$']]                [the match suffix]]
52    [[[^$$]]                [a literal `'$'` character]]
53]
54
55Any other sequence beginning with `'$'` simply represents itself. For example, if the format string were
56`"$a"` then `"$a"` would be inserted into the output sequence.
57
58[h2 Replace Options]
59
60The _regex_replace_ algorithm takes an optional bitmask parameter to control the formatting. The
61possible values of the bitmask are:
62
63[table Format Flags
64    [[Flag]                     [Meaning]]
65    [[`format_first_only`]      [Only replace the first match, not all of them.]]
66    [[`format_no_copy`]         [Don't copy the parts of the input sequence that didn't match the regex to the output sequence.]]
67    [[`format_literal`]         [Treat the format string as a literal; that is, don't recognize any escape sequences.]]
68]
69
70These flags live in the `regex_constants` namespace.
71
72[endsect]
Note: See TracBrowser for help on using the repository browser.