For an introduction to regular expressions, see regular expressions.
i1 : replace("[a-z]+", "x", "Dog cat cat.") o1 = Dx x x. |
The replacement string may contain formatting syntax such as backreferences $1 or \\1, which will be replaced by the string matching the corresponding parenthesized subexpression of re.
i2 : replace("(\\w+)\\.?", "A \\1.", "Dog cat cat.") o2 = A Dog. A cat. A cat. |
Special operators such as the lowercase operator \\L may also be used to manipulate the replacement substring.
i3 : replace("(\\w+)\\.?", "A \\L$1.", "Dog cat cat.") o3 = A dog. A cat. A cat. |
Lookaheads and lookbehinds can be used to precisely control the regular expression matches.
i4 : s = "catfish cats dogs"; |
i5 : replace("cat(?!fish)s?", "\\U$&", s) o5 = catfish CATS dogs |
i6 : replace("\\w+(?=s\\b)", "\\U$&", s) o6 = catfish CATs DOGs |
i7 : s = "goldfish swordfish catfish catdog"; |
i8 : replace("\\w+(?=fish)", "\\U$&", s) o8 = GOLDfish SWORDfish CATfish catdog |
i9 : replace("(?<=cat)\\w+", "\\U$&", s) o9 = goldfish swordfish catFISH catDOG |
The POSIX => true option can be used to specify the POSIX Extended flavor for the regular expression used to match. Note that only the Perl flavor allows the use of lookaheads and lookbehinds.