类 Perl5Substitution
- 所有已实现的接口:
Substitution
Util.substitute.
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:
-
Suppose you have the pattern b\d+: and you want to substitute
the b's for a's and the colon for a dash in parts of
your input matching the pattern. You can do this by changing the
pattern to b(\d+): and using the substitution expression
a$1-. When a substitution is made, the $1 means
"Substitute whatever was matched by the first saved group of the
matching pattern." An input of b123: after substitution
would yield a result of a123-. But there's a little more
to be aware of. If you set the numInterpolations parameter to
INTERPOLATE_ALL, then every time a match is found, the
interpolation variables are computed relative to that match.
But if numInterpolations is set to some positive integer, then
only the interpolation variables for the first numInterpolations
matches are computed relative to the most recent match. After that,
the remaining substitutions have their variable interpolations performed
relative to the numInterpolations 'th match. So using the
previously mentioned pattern and substitution expression, if you have
an input of
Tank b123: 85 Tank b256: 32 Tank b78: 22and use a numInterpolations value of INTERPOLATE_ALL and numSubs value (see
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:
- \\U
- make substitution uppercase until end of substitution or \\E
- \\u
- make next character uppercase
- \\L
- make substitution uppercase until end of substitution or \\E
- \\l
- make next character uppercase
- \\E
- mark the end of the case modification
- 从以下版本开始:
- 1.1
- 版本:
- @version@
- 另请参阅:
-
字段概要
字段修饰符和类型字段说明static final intA constant used when creating a Perl5Substitution indicating that interpolation variables should be computed relative to the most recent pattern match.static final intA constant used when creating a Perl5Substitution indicating that interpolation variables should be interpreted literally, effectively disabling interpolation. -
构造器概要
构造器构造器说明Default constructor initializing substitution to a zero length String and the number of interpolations toINTERPOLATE_ALL.Perl5Substitution(String substitution) Creates a Perl5Substitution using the specified substitution and setting the number of interpolations toINTERPOLATE_ALL.Perl5Substitution(String substitution, int numInterpolations) Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to the specified value. -
方法概要
修饰符和类型方法说明voidappendSubstitution(StringBuffer appendBuffer, MatchResult match, int substitutionCount, PatternMatcherInput originalInput, PatternMatcher matcher, Pattern pattern) Appends the substitution to a buffer containing the original input with substitutions applied for the pattern matches found so far.voidsetSubstitution(String substitution) Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations toINTERPOLATE_ALL.voidsetSubstitution(String substitution, int numInterpolations) Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations to the specified value.从类继承的方法 org.apache.oro.text.regex.StringSubstitution
getSubstitution, toString
-
字段详细资料
-
INTERPOLATE_ALL
public static final int INTERPOLATE_ALLA constant used when creating a Perl5Substitution indicating that interpolation variables should be computed relative to the most recent pattern match.- 另请参阅:
-
INTERPOLATE_NONE
public static final int INTERPOLATE_NONEA constant used when creating a Perl5Substitution indicating that interpolation variables should be interpreted literally, effectively disabling interpolation.- 另请参阅:
-
-
构造器详细资料
-
Perl5Substitution
public Perl5Substitution()Default constructor initializing substitution to a zero length String and the number of interpolations toINTERPOLATE_ALL. -
Perl5Substitution
Creates a Perl5Substitution using the specified substitution and setting the number of interpolations toINTERPOLATE_ALL.- 参数:
substitution- The string to use as a substitution.
-
Perl5Substitution
Creates a Perl5Substitution using the specified substitution and setting the number of interpolations to the specified value.- 参数:
substitution- The string to use as a substitution.numInterpolations- If set to INTERPOLATE_NONE, interpolation variables are interpreted literally and not as references to the saved parenthesized groups of a pattern match. If set to INTERPOLATE_ALL , all variable interpolations are computed relative to the pattern match responsible for the current substitution. If set to a positive integer, the first numInterpolations substitutions have their variable interpolation performed relative to the most recent match, but the remaining substitutions have their variable interpolations performed relative to the numInterpolations 'th match.
-
-
方法详细资料
-
setSubstitution
Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations toINTERPOLATE_ALL. You should use this method in order to avoid repeatedly allocating new Perl5Substitutions. It is recommended that you allocate a single Perl5Substitution and reuse it by using this method when appropriate.- 覆盖:
setSubstitution在类中StringSubstitution- 参数:
substitution- The string to use as a substitution.
-
setSubstitution
Sets the substitution represented by this Perl5Substitution, also setting the number of interpolations to the specified value. You should use this method in order to avoid repeatedly allocating new Perl5Substitutions. It is recommended that you allocate a single Perl5Substitution and reuse it by using this method when appropriate.- 参数:
substitution- The string to use as a substitution.numInterpolations- If set to INTERPOLATE_NONE, interpolation variables are interpreted literally and not as references to the saved parenthesized groups of a pattern match. If set to INTERPOLATE_ALL , all variable interpolations are computed relative to the pattern match responsible for the current substitution. If set to a positive integer, the first numInterpolations substitutions have their variable interpolation performed relative to the most recent match, but the remaining substitutions have their variable interpolations performed relative to the numInterpolations 'th match.
-
appendSubstitution
public void appendSubstitution(StringBuffer appendBuffer, MatchResult match, int substitutionCount, PatternMatcherInput originalInput, PatternMatcher matcher, Pattern pattern) Appends the substitution to a buffer containing the original input with substitutions applied for the pattern matches found so far. SeeSubstitution.appendSubstition()for more details regarding the expected behavior of this method.- 指定者:
appendSubstitution在接口中Substitution- 覆盖:
appendSubstitution在类中StringSubstitution- 参数:
appendBuffer- The buffer containing the new string resulting from performing substitutions on the original input.match- The current match causing a substitution to be made.substitutionCount- The number of substitutions that have been performed so far by Util.substitute.originalInput- The original input upon which the substitutions are being performed. This is a read-only parameter and is not modified.matcher- The PatternMatcher used to find the current match.pattern- The Pattern used to find the current match.
-