Replacing Pattern Matches
The overloaded replace() methods replace occurrences of a regular expression pattern in an input string with a given replacement string. A count argument allows you to specify how many matches are replaced. By default, only the first match is replaced; specifying a count of 0 replaces all occurrences of the pattern. A matchID argument names the subexpression match that is replaced; the default value is 0, which replaces the overall match. For instance, this code replaces the first five occurrences of pattern in str with replacement:
 
RWURegularExpression r(pattern);
size_t num = r.replace(str, replacement, 5, 0);
The function replace() returns the number of occurrences of the pattern that are actually replaced. For example, if str contains only three occurrences of pattern, then num equals 3 in the code above.
Overloads of replace() enable you to specify the start and end positions in the input string for the replace operation. Positions are specified using RWUConstStringIterator instances. Thus, this code replaces all occurrences of pattern in str from the beginning of the string to position 25:
 
RWURegularExpression r(pattern);
RWUConstStringIterator start = str.beginCodePointIterator();
RWUConstStringIterator end =
str.beginCodePointIterator().advanceCodePoints(25);
 
size_t num = r.replace(str, replacement, 0, 0, start, end);
Finally, the Boolean replaceEmptyMatches argument allows you to specify whether or not empty (zero-length) matches should be replaced. The default is true. For example, this code sets replaceEmptyMatches to false:
 
size_t num = r.replace(str,
replacement,
0,
0,
str.beginCodePointIterator(),
str.endCodePointIterator(),
false);