The substitution string may contain variable interpolations referring to the saved parenthesized groups of the search pattern. A variable interpolation is denoted by $1, or $2, or $3, etc. If you want such expressions to be interpreted literally, you should set the numInterpolations parameter to INTERPOLATE_NONE . It is easiest to explain what an interpolated variable does by giving an example:
Tank b123: 85 Tank b256: 32 Tank b78: 22and use a numInterpolations value of INTERPOLATE_ALL and numSubs value (see {@link Util#substitute Util.substitute}) of SUBSTITUTE_ALL, then your result will be:
Tank a123- 85 Tank a256- 32 Tank a78- 22But if you set numInterpolations to 2 and keep numSubs with a value of SUBSTITUTE_ALL, your result is:
Tank a123- 85 Tank a256- 32 Tank a256- 22Notice how the last substitution uses the same value for $1 as the second substitution.
A final thing to keep in mind is that if you use an interpolation variable that corresponds to a group not contained in the match, then it is interpreted as the empty string. So given the regular expression from the example, and a substitution expression of a$2-, the result of the last sample input would be:
Tank a- 85 Tank a- 32 Tank a- 22The special substitution $& will interpolate the entire portion of the input matched by the regular expression. $0 will do the same, but it is recommended that it be avoided because the latest versions of Perl use $0 to store the program name rather than duplicate the behavior of $&. Also, the result of substituting $ followed by a non-positive integer is undefined. In order to include a $ in a substitution, it should be escaped with a backslash (e.g., "\\$0").
Perl5 double-quoted string case modification is also supported in the substitution. The following escape sequences are supported:
The substitution string may contain variable interpolations referring to the saved parenthesized groups of the search pattern. A variable interpolation is denoted by $1, or $2, or $3, etc. If you want such expressions to be interpreted literally, you should set the numInterpolations parameter to INTERPOLATE_NONE . It is easiest to explain what an interpolated variable does by giving an example:
Tank b123: 85 Tank b256: 32 Tank b78: 22and use a numInterpolations value of INTERPOLATE_ALL and numSubs value (see {@link Util#substitute Util.substitute}) of SUBSTITUTE_ALL, then your result will be:
Tank a123- 85 Tank a256- 32 Tank a78- 22But if you set numInterpolations to 2 and keep numSubs with a value of SUBSTITUTE_ALL, your result is:
Tank a123- 85 Tank a256- 32 Tank a256- 22Notice how the last substitution uses the same value for $1 as the second substitution.
A final thing to keep in mind is that if you use an interpolation variable that corresponds to a group not contained in the match, then it is interpreted as the empty string. So given the regular expression from the example, and a substitution expression of a$2-, the result of the last sample input would be:
Tank a- 85 Tank a- 32 Tank a- 22The special substitution $& will interpolate the entire portion of the input matched by the regular expression. $0 will do the same, but it is recommended that it be avoided because the latest versions of Perl use $0 to store the program name rather than duplicate the behavior of $&. Also, the result of substituting $ followed by a non-positive integer is undefined. In order to include a $ in a substitution, it should be escaped with a backslash (e.g., "\\$0").
Perl5 double-quoted string case modification is also supported in the substitution. The following escape sequences are supported:
|
|
|
|