Replacement Strings
Previous  Top  Next



NOTE: replace is not supported natively by PCRE: it is instead a feature provided by the Delphi wrappers.



Substitutions are the only special constructs recognized in a replacement pattern. Substitutions variables are always prefixed by a single $ character followed by either a number or a special meta character.

The following table shows how to define replacement patterns.

Character
Meaning

$123
Substitutes the last substring matched by group number 123 (decimal).

${name}
Substitutes the last substring matched by named group 'name'

$$
Substitutes a single "$" literal.

$&
Substitutes a copy of the entire match itself.

$`
Substitutes all the text of the input string before the match.

$'
Substitutes all the text of the input string after the match.

$+
Substitutes the last group captured.

$_
Substitutes the entire input string

 

When using a substitutions variable like $n, an exception is raised if n > of the index of the last capturing group.


This is different from the behaviour of .NET Regex classes, which instead simply proceed to consider the variable as a literal value. If you want to really introduce a literal '$' followed by a numerical value use a double dollar character.