Functions for Searching and Replacing in Strings
Functions for searching and are described separately.
Replaces the first occurrence, if it exists, of the ‘pattern’ substring in ‘haystack’ with the ‘replacement’ substring.
Hereafter, ‘pattern’ and ‘replacement’ must be constants.
Replacement using the ‘pattern’ regular expression. A re2 regular expression.
Replaces only the first occurrence, if it exists.
A pattern can be specified as ‘replacement’. This pattern can include substitutions .
The substitution \0
includes the entire regular expression. Substitutions \1-\9
correspond to the subpattern numbers.To use the \
character in a template, escape it using \
.
Also keep in mind that a string literal requires an extra escape.
Example 1. Converting the date to American format:
2014-03-17 03/17/2014
2014-03-19 03/19/2014
2014-03-20 03/20/2014
2014-03-21 03/21/2014
2014-03-22 03/22/2014
2014-03-23 03/23/2014
┌─res────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
└────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
This does the same thing, but replaces all the occurrences. Example:
┌─res────────────────────────┐
│ HHeelllloo,, WWoorrlldd!! │
└────────────────────────────┘
As an exception, if a regular expression worked on an empty substring, the replacement is not made more than once.
Example:
┌─res─────────────────┐
│ here: Hello, World! │