java.util.regex
Class Matcher

java.lang.Object sample code for java.lang.Object definition code for java.lang.Object 
  extended by java.util.regex.Matcher
All Implemented Interfaces:
MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult

public final class Matcher
extends Object sample code for java.lang.Object definition code for java.lang.Object
implements MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult

An engine that performs match operations on a character sequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence by interpreting a Pattern sample code for java.util.regex.Pattern definition code for java.util.regex.Pattern .

A matcher is created from a pattern by invoking the pattern's matcher sample code for java.util.regex.Pattern.matcher(java.lang.CharSequence) definition code for java.util.regex.Pattern.matcher(java.lang.CharSequence) method. Once created, a matcher can be used to perform three different kinds of match operations:

Each of these methods returns a boolean indicating success or failure. More information about a successful match can be obtained by querying the state of the matcher.

A matcher finds matches in a subset of its input called the region. By default, the region contains all of the matcher's input. The region can be modified via theregion sample code for java.util.regex.Matcher.region(int, int) definition code for java.util.regex.Matcher.region(int, int) method and queried via the regionStart sample code for java.util.regex.Matcher.regionStart() definition code for java.util.regex.Matcher.regionStart() and regionEnd sample code for java.util.regex.Matcher.regionEnd() definition code for java.util.regex.Matcher.regionEnd() methods. The way that the region boundaries interact with some pattern constructs can be changed. See useAnchoringBounds sample code for java.util.regex.Matcher.useAnchoringBounds(boolean) definition code for java.util.regex.Matcher.useAnchoringBounds(boolean) and useTransparentBounds sample code for java.util.regex.Matcher.useTransparentBounds(boolean) definition code for java.util.regex.Matcher.useTransparentBounds(boolean) for more details.

This class also defines methods for replacing matched subsequences with new strings whose contents can, if desired, be computed from the match result. The appendReplacement sample code for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String) definition code for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String) and appendTail sample code for java.util.regex.Matcher.appendTail(java.lang.StringBuffer) definition code for java.util.regex.Matcher.appendTail(java.lang.StringBuffer) methods can be used in tandem in order to collect the result into an existing string buffer, or the more convenient replaceAll sample code for java.util.regex.Matcher.replaceAll(java.lang.String) definition code for java.util.regex.Matcher.replaceAll(java.lang.String) method can be used to create a string in which every matching subsequence in the input sequence is replaced.

The explicit state of a matcher includes the start and end indices of the most recent successful match. It also includes the start and end indices of the input subsequence captured by each capturing group in the pattern as well as a total count of such subsequences. As a convenience, methods are also provided for returning these captured subsequences in string form.

The explicit state of a matcher is initially undefined; attempting to query any part of it before a successful match will cause an IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException to be thrown. The explicit state of a matcher is recomputed by every match operation.

The implicit state of a matcher includes the input character sequence as well as the append position, which is initially zero and is updated by the appendReplacement sample code for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String) definition code for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String) method.

A matcher may be reset explicitly by invoking its reset() sample code for java.util.regex.Matcher.reset() definition code for java.util.regex.Matcher.reset() method or, if a new input sequence is desired, its reset(CharSequence) sample code for java.util.regex.Matcher.reset(java.lang.CharSequence) definition code for java.util.regex.Matcher.reset(java.lang.CharSequence) method. Resetting a matcher discards its explicit state information and sets the append position to zero.

Instances of this class are not safe for use by multiple concurrent threads.

Since:
1.4

Method Summary
 Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher appendReplacement sample code for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String) definition code for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String) (StringBuffer sample code for java.lang.StringBuffer definition code for java.lang.StringBuffer  sb, String sample code for java.lang.String definition code for java.lang.String  replacement)
          Implements a non-terminal append-and-replace step.
 StringBuffer sample code for java.lang.StringBuffer definition code for java.lang.StringBuffer appendTail sample code for java.util.regex.Matcher.appendTail(java.lang.StringBuffer) definition code for java.util.regex.Matcher.appendTail(java.lang.StringBuffer) (StringBuffer sample code for java.lang.StringBuffer definition code for java.lang.StringBuffer  sb)
          Implements a terminal append-and-replace step.
 int end sample code for java.util.regex.Matcher.end() definition code for java.util.regex.Matcher.end() ()
          Returns the offset after the last character matched.
 int end sample code for java.util.regex.Matcher.end(int) definition code for java.util.regex.Matcher.end(int) (int group)
          Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.
 boolean find sample code for java.util.regex.Matcher.find() definition code for java.util.regex.Matcher.find() ()
          Attempts to find the next subsequence of the input sequence that matches the pattern.
 boolean find sample code for java.util.regex.Matcher.find(int) definition code for java.util.regex.Matcher.find(int) (int start)
          Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
 String sample code for java.lang.String definition code for java.lang.String group sample code for java.util.regex.Matcher.group() definition code for java.util.regex.Matcher.group() ()
          Returns the input subsequence matched by the previous match.
 String sample code for java.lang.String definition code for java.lang.String group sample code for java.util.regex.Matcher.group(int) definition code for java.util.regex.Matcher.group(int) (int group)
          Returns the input subsequence captured by the given group during the previous match operation.
 int groupCount sample code for java.util.regex.Matcher.groupCount() definition code for java.util.regex.Matcher.groupCount() ()
          Returns the number of capturing groups in this matcher's pattern.
 boolean hasAnchoringBounds sample code for java.util.regex.Matcher.hasAnchoringBounds() definition code for java.util.regex.Matcher.hasAnchoringBounds() ()
          Queries the anchoring of region bounds for this matcher.
 boolean hasTransparentBounds sample code for java.util.regex.Matcher.hasTransparentBounds() definition code for java.util.regex.Matcher.hasTransparentBounds() ()
          Queries the transparency of region bounds for this matcher.
 boolean hitEnd sample code for java.util.regex.Matcher.hitEnd() definition code for java.util.regex.Matcher.hitEnd() ()
          Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher.
 boolean lookingAt sample code for java.util.regex.Matcher.lookingAt() definition code for java.util.regex.Matcher.lookingAt() ()
          Attempts to match the input sequence, starting at the beginning of the region, against the pattern.
 boolean matches sample code for java.util.regex.Matcher.matches() definition code for java.util.regex.Matcher.matches() ()
          Attempts to match the entire region against the pattern.
 Pattern sample code for java.util.regex.Pattern definition code for java.util.regex.Pattern pattern sample code for java.util.regex.Matcher.pattern() definition code for java.util.regex.Matcher.pattern() ()
          Returns the pattern that is interpreted by this matcher.
static String sample code for java.lang.String definition code for java.lang.String quoteReplacement sample code for java.util.regex.Matcher.quoteReplacement(java.lang.String) definition code for java.util.regex.Matcher.quoteReplacement(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  s)
          Returns a literal replacement String for the specified String.
 Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher region sample code for java.util.regex.Matcher.region(int, int) definition code for java.util.regex.Matcher.region(int, int) (int start, int end)
          Sets the limits of this matcher's region.
 int regionEnd sample code for java.util.regex.Matcher.regionEnd() definition code for java.util.regex.Matcher.regionEnd() ()
          Reports the end index (exclusive) of this matcher's region.
 int regionStart sample code for java.util.regex.Matcher.regionStart() definition code for java.util.regex.Matcher.regionStart() ()
          Reports the start index of this matcher's region.
 String sample code for java.lang.String definition code for java.lang.String replaceAll sample code for java.util.regex.Matcher.replaceAll(java.lang.String) definition code for java.util.regex.Matcher.replaceAll(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  replacement)
          Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
 String sample code for java.lang.String definition code for java.lang.String replaceFirst sample code for java.util.regex.Matcher.replaceFirst(java.lang.String) definition code for java.util.regex.Matcher.replaceFirst(java.lang.String) (String sample code for java.lang.String definition code for java.lang.String  replacement)
          Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
 boolean requireEnd sample code for java.util.regex.Matcher.requireEnd() definition code for java.util.regex.Matcher.requireEnd() ()
          Returns true if more input could change a positive match into a negative one.
 Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher reset sample code for java.util.regex.Matcher.reset() definition code for java.util.regex.Matcher.reset() ()
          Resets this matcher.
 Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher reset sample code for java.util.regex.Matcher.reset(java.lang.CharSequence) definition code for java.util.regex.Matcher.reset(java.lang.CharSequence) (CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  input)
          Resets this matcher with a new input sequence.
 int start sample code for java.util.regex.Matcher.start() definition code for java.util.regex.Matcher.start() ()
          Returns the start index of the previous match.
 int start sample code for java.util.regex.Matcher.start(int) definition code for java.util.regex.Matcher.start(int) (int group)
          Returns the start index of the subsequence captured by the given group during the previous match operation.
 MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult toMatchResult sample code for java.util.regex.Matcher.toMatchResult() definition code for java.util.regex.Matcher.toMatchResult() ()
          Returns the match state of this matcher as a MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult .
 String sample code for java.lang.String definition code for java.lang.String toString sample code for java.util.regex.Matcher.toString() definition code for java.util.regex.Matcher.toString() ()
          Returns the string representation of this matcher.
 Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher useAnchoringBounds sample code for java.util.regex.Matcher.useAnchoringBounds(boolean) definition code for java.util.regex.Matcher.useAnchoringBounds(boolean) (boolean b)
          Sets the anchoring of region bounds for this matcher.
 Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher usePattern sample code for java.util.regex.Matcher.usePattern(java.util.regex.Pattern) definition code for java.util.regex.Matcher.usePattern(java.util.regex.Pattern) (Pattern sample code for java.util.regex.Pattern definition code for java.util.regex.Pattern  newPattern)
          Changes the Pattern that this Matcher uses to find matches with.
 Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher useTransparentBounds sample code for java.util.regex.Matcher.useTransparentBounds(boolean) definition code for java.util.regex.Matcher.useTransparentBounds(boolean) (boolean b)
          Sets the transparency of region bounds for this matcher.
 
Methods inherited from class java.lang.Object sample code for java.lang.Object definition code for java.lang.Object
clone sample code for java.lang.Object.clone() definition code for java.lang.Object.clone() , equals sample code for java.lang.Object.equals(java.lang.Object) definition code for java.lang.Object.equals(java.lang.Object) , finalize sample code for java.lang.Object.finalize() definition code for java.lang.Object.finalize() , getClass sample code for java.lang.Object.getClass() definition code for java.lang.Object.getClass() , hashCode sample code for java.lang.Object.hashCode() definition code for java.lang.Object.hashCode() , notify sample code for java.lang.Object.notify() definition code for java.lang.Object.notify() , notifyAll sample code for java.lang.Object.notifyAll() definition code for java.lang.Object.notifyAll() , wait sample code for java.lang.Object.wait() definition code for java.lang.Object.wait() , wait sample code for java.lang.Object.wait(long) definition code for java.lang.Object.wait(long) , wait sample code for java.lang.Object.wait(long, int) definition code for java.lang.Object.wait(long, int)
 

Method Detail

pattern sample code for java.util.regex.Matcher.pattern() definition code for java.util.regex.Matcher.pattern()

public Pattern sample code for java.util.regex.Pattern definition code for java.util.regex.Pattern  pattern()
Returns the pattern that is interpreted by this matcher.

Returns:
The pattern for which this matcher was created

toMatchResult sample code for java.util.regex.Matcher.toMatchResult() definition code for java.util.regex.Matcher.toMatchResult()

public MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult  toMatchResult()
Returns the match state of this matcher as a MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult . The result is unaffected by subsequent operations performed upon this matcher.

Returns:
a MatchResult with the state of this matcher

usePattern sample code for java.util.regex.Matcher.usePattern(java.util.regex.Pattern) definition code for java.util.regex.Matcher.usePattern(java.util.regex.Pattern)

public Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher  usePattern(Pattern sample code for java.util.regex.Pattern definition code for java.util.regex.Pattern  newPattern)
Changes the Pattern that this Matcher uses to find matches with.

This method causes this matcher to lose information about the groups of the last match that occurred. The matcher's position in the input is maintained and its last append position is unaffected.

Parameters:
newPattern - The new pattern used by this matcher
Returns:
This matcher
Throws:
IllegalArgumentException sample code for java.lang.IllegalArgumentException definition code for java.lang.IllegalArgumentException - If newPattern is null
Since:
1.5

reset sample code for java.util.regex.Matcher.reset() definition code for java.util.regex.Matcher.reset()

public Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher  reset()
Resets this matcher.

Resetting a matcher discards all of its explicit state information and sets its append position to zero. The matcher's region is set to the default region, which is its entire character sequence. The anchoring and transparency of this matcher's region boundaries are unaffected.

Returns:
This matcher

reset sample code for java.util.regex.Matcher.reset(java.lang.CharSequence) definition code for java.util.regex.Matcher.reset(java.lang.CharSequence)

public Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher  reset(CharSequence sample code for java.lang.CharSequence definition code for java.lang.CharSequence  input)
Resets this matcher with a new input sequence.

Resetting a matcher discards all of its explicit state information and sets its append position to zero. The matcher's region is set to the default region, which is its entire character sequence. The anchoring and transparency of this matcher's region boundaries are unaffected.

Parameters:
input - The new input character sequence
Returns:
This matcher

start sample code for java.util.regex.Matcher.start() definition code for java.util.regex.Matcher.start()

public int start()
Returns the start index of the previous match.

Specified by:
start sample code for java.util.regex.MatchResult.start() definition code for java.util.regex.MatchResult.start() in interface MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult
Returns:
The index of the first character matched
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

start sample code for java.util.regex.Matcher.start(int) definition code for java.util.regex.Matcher.start(int)

public int start(int group)
Returns the start index of the subsequence captured by the given group during the previous match operation.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.start(0) is equivalent to m.start().

Specified by:
start sample code for java.util.regex.MatchResult.start(int) definition code for java.util.regex.MatchResult.start(int) in interface MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
The index of the first character captured by the group, or -1 if the match was successful but the group itself did not match anything
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException definition code for java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

end sample code for java.util.regex.Matcher.end() definition code for java.util.regex.Matcher.end()

public int end()
Returns the offset after the last character matched.

Specified by:
end sample code for java.util.regex.MatchResult.end() definition code for java.util.regex.MatchResult.end() in interface MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult
Returns:
The offset after the last character matched
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

end sample code for java.util.regex.Matcher.end(int) definition code for java.util.regex.Matcher.end(int)

public int end(int group)
Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.end(0) is equivalent to m.end().

Specified by:
end sample code for java.util.regex.MatchResult.end(int) definition code for java.util.regex.MatchResult.end(int) in interface MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
The offset after the last character captured by the group, or -1 if the match was successful but the group itself did not match anything
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException definition code for java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

group sample code for java.util.regex.Matcher.group() definition code for java.util.regex.Matcher.group()

public String sample code for java.lang.String definition code for java.lang.String  group()
Returns the input subsequence matched by the previous match.

For a matcher m with input sequence s, the expressions m.group() and s.substring(m.start(), m.end()) are equivalent.

Note that some patterns, for example a*, match the empty string. This method will return the empty string when the pattern successfully matches the empty string in the input.

Specified by:
group sample code for java.util.regex.MatchResult.group() definition code for java.util.regex.MatchResult.group() in interface MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult
Returns:
The (possibly empty) subsequence matched by the previous match, in string form
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

group sample code for java.util.regex.Matcher.group(int) definition code for java.util.regex.Matcher.group(int)

public String sample code for java.lang.String definition code for java.lang.String  group(int group)
Returns the input subsequence captured by the given group during the previous match operation.

For a matcher m, input sequence s, and group index g, the expressions m.group(g) and s.substring(m.start(g), m.end(g)) are equivalent.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group().

If the match was successful but the group specified failed to match any part of the input sequence, then null is returned. Note that some groups, for example (a*), match the empty string. This method will return the empty string when such a group successfully matches the empty string in the input.

Specified by:
group sample code for java.util.regex.MatchResult.group(int) definition code for java.util.regex.MatchResult.group(int) in interface MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
The (possibly empty) subsequence captured by the group during the previous match, or null if the group failed to match part of the input
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException definition code for java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

groupCount sample code for java.util.regex.Matcher.groupCount() definition code for java.util.regex.Matcher.groupCount()

public int groupCount()
Returns the number of capturing groups in this matcher's pattern.

Group zero denotes the entire pattern by convention. It is not included in this count.

Any non-negative integer smaller than or equal to the value returned by this method is guaranteed to be a valid group index for this matcher.

Specified by:
groupCount sample code for java.util.regex.MatchResult.groupCount() definition code for java.util.regex.MatchResult.groupCount() in interface MatchResult sample code for java.util.regex.MatchResult definition code for java.util.regex.MatchResult
Returns:
The number of capturing groups in this matcher's pattern

matches sample code for java.util.regex.Matcher.matches() definition code for java.util.regex.Matcher.matches()

public boolean matches()
Attempts to match the entire region against the pattern.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:
true if, and only if, the entire region sequence matches this matcher's pattern

find sample code for java.util.regex.Matcher.find() definition code for java.util.regex.Matcher.find()

public boolean find()
Attempts to find the next subsequence of the input sequence that matches the pattern.

This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:
true if, and only if, a subsequence of the input sequence matches this matcher's pattern

find sample code for java.util.regex.Matcher.find(int) definition code for java.util.regex.Matcher.find(int)

public boolean find(int start)
Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.

If the match succeeds then more information can be obtained via the start, end, and group methods, and subsequent invocations of the find() sample code for java.util.regex.Matcher.find() definition code for java.util.regex.Matcher.find() method will start at the first character not matched by this match.

Returns:
true if, and only if, a subsequence of the input sequence starting at the given index matches this matcher's pattern
Throws:
IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException definition code for java.lang.IndexOutOfBoundsException - If start is less than zero or if start is greater than the length of the input sequence.

lookingAt sample code for java.util.regex.Matcher.lookingAt() definition code for java.util.regex.Matcher.lookingAt()

public boolean lookingAt()
Attempts to match the input sequence, starting at the beginning of the region, against the pattern.

Like the matches sample code for java.util.regex.Matcher.matches() definition code for java.util.regex.Matcher.matches() method, this method always starts at the beginning of the region; unlike that method, it does not require that the entire region be matched.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:
true if, and only if, a prefix of the input sequence matches this matcher's pattern

quoteReplacement sample code for java.util.regex.Matcher.quoteReplacement(java.lang.String) definition code for java.util.regex.Matcher.quoteReplacement(java.lang.String)

public static String sample code for java.lang.String definition code for java.lang.String  quoteReplacement(String sample code for java.lang.String definition code for java.lang.String  s)
Returns a literal replacement String for the specified String. This method produces a String that will work use as a literal replacement s in the appendReplacement method of the Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.

Parameters:
s - The string to be literalized
Returns:
A literal string replacement
Since:
1.5

appendReplacement sample code for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String) definition code for java.util.regex.Matcher.appendReplacement(java.lang.StringBuffer, java.lang.String)

public Matcher sample code for java.util.regex.Matcher definition code for java.util.regex.Matcher  appendReplacement(StringBuffer sample code for java.lang.StringBuffer definition code for java.lang.StringBuffer  sb,
                                 String sample code for java.lang.String definition code for java.lang.String  replacement)
Implements a non-terminal append-and-replace step.

This method performs the following actions:

  1. It reads characters from the input sequence, starting at the append position, and appends them to the given string buffer. It stops after reading the last character preceding the previous match, that is, the character at index start() sample code for java.util.regex.Matcher.start() definition code for java.util.regex.Matcher.start()  - 1.

  2. It appends the given replacement string to the string buffer.

  3. It sets the append position of this matcher to the index of the last character matched, plus one, that is, to end() sample code for java.util.regex.Matcher.end() definition code for java.util.regex.Matcher.end() .

The replacement string may contain references to subsequences captured during the previous match: Each occurrence of $g will be replaced by the result of evaluating group sample code for java.util.regex.Matcher.group(int) definition code for java.util.regex.Matcher.group(int) (g). The first number after the $ is always treated as part of the group reference. Subsequent numbers are incorporated into g if they would form a legal group reference. Only the numerals '0' through '9' are considered as potential components of the group reference. If the second group matched the string "foo", for example, then passing the replacement string "$2bar" would cause "foobar" to be appended to the string buffer. A dollar sign ($) may be included as a literal in the replacement string by preceding it with a backslash (\$).

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

This method is intended to be used in a loop together with the appendTail sample code for java.util.regex.Matcher.appendTail(java.lang.StringBuffer) definition code for java.util.regex.Matcher.appendTail(java.lang.StringBuffer) and find sample code for java.util.regex.Matcher.find() definition code for java.util.regex.Matcher.find() methods. The following code, for example, writes one dog two dogs in the yard to the standard-output stream:

 Pattern p = Pattern.compile("cat");
 Matcher m = p.matcher("one cat two cats in the yard");
 StringBuffer sb = new StringBuffer();
 while (m.find()) {
     m.appendReplacement(sb, "dog");
 }
 m.appendTail(sb);
 System.out.println(sb.toString());

Parameters:
sb - The target string buffer
replacement - The replacement string
Returns:
This matcher
Throws:
IllegalStateException sample code for java.lang.IllegalStateException definition code for java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException sample code for java.lang.IndexOutOfBoundsException