| 1 | [section String Substitutions] |
|---|
| 2 | |
|---|
| 3 | Regular expressions are not only good for searching text; they're good at ['manipulating] it. And one of the most |
|---|
| 4 | common text manipulation tasks is search-and-replace. xpressive provides the _regex_replace_ algorithm for |
|---|
| 5 | searching and replacing. |
|---|
| 6 | |
|---|
| 7 | [h2 regex_replace()] |
|---|
| 8 | |
|---|
| 9 | Performing search-and-replace using _regex_replace_ is simple. All you need is an input sequence, a regex object, |
|---|
| 10 | and a format string. There are two versions of the _regex_replace_ algorithm. The first accepts the input |
|---|
| 11 | sequence as `std::basic_string<>` and returns the result in a new `std::basic_string<>`. The second accepts |
|---|
| 12 | the input sequence as a pair of iterators, and writes the result into an output iterator. Below are examples of |
|---|
| 13 | each. |
|---|
| 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 | |
|---|
| 27 | The above program prints out the following: |
|---|
| 28 | |
|---|
| 29 | [pre |
|---|
| 30 | Ther is her face |
|---|
| 31 | Ther is her face |
|---|
| 32 | ] |
|---|
| 33 | |
|---|
| 34 | Notice that ['all] the occurrences of `"his"` have been replaced with `"her"`. |
|---|
| 35 | |
|---|
| 36 | Click [link boost_xpressive.user_s_guide.examples.replace_all_sub_strings_that_match_a_regex here] to see a complete |
|---|
| 37 | example program that shows how to use _regex_replace_. And check the _regex_replace_ reference |
|---|
| 38 | to see a complete list of the available overloads. |
|---|
| 39 | |
|---|
| 40 | [h2 The Format String] |
|---|
| 41 | |
|---|
| 42 | As with Perl, you can refer to sub-matches in the format string. The table below shows the escape sequences |
|---|
| 43 | xpressive 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 | |
|---|
| 55 | Any 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 | |
|---|
| 60 | The _regex_replace_ algorithm takes an optional bitmask parameter to control the formatting. The |
|---|
| 61 | possible 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 | |
|---|
| 70 | These flags live in the `regex_constants` namespace. |
|---|
| 71 | |
|---|
| 72 | [endsect] |
|---|