java.util.regex
Class Matcher

java.lang.Object
  extended byjava.util.regex.Matcher

public final class Matcher
extends Object

An engine that performs match operations on a character sequence by interpreting a Pattern.

A matcher is created from a pattern by invoking the pattern's matcher 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.

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 and appendTail methods can be used in tandem in order to collect the result into an existing string buffer, or the more convenient replaceAll 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 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 method.

A matcher may be reset explicitly by invoking its reset() method or, if a new input sequence is desired, its reset(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
Author:
Mike McCloskey, Mark Reinhold, JSR-51 Expert Group

Field Summary
(package private)  int acceptMode
           
(package private) static int ENDANCHOR
          Matcher state used by the last node.
(package private)  int first
          The range of string that last matched the pattern.
(package private)  int from
          The range within the string that is to be matched.
(package private)  int[] groups
          The storage used by groups.
(package private)  int last
          The range of string that last matched the pattern.
(package private)  int lastAppendPosition
          The index of the last position appended in a substitution.
(package private)  int[] locals
          Storage used by nodes to tell what repetition they are on in a pattern, and where groups begin.
(package private) static int NOANCHOR
           
(package private)  int oldLast
          The end index of what matched in the last match operation.
(package private)  Pattern parentPattern
          The Pattern object that created this Matcher.
(package private)  CharSequence text
          The original string being matched.
(package private)  int to
          The range within the string that is to be matched.
 
Constructor Summary
(package private) Matcher()
          No default constructor.
(package private) Matcher(Pattern parent, CharSequence text)
          All matchers have the state used by Pattern during a match.
 
Method Summary
 Matcher appendReplacement(StringBuffer sb, String replacement)
          Implements a non-terminal append-and-replace step.
 StringBuffer appendTail(StringBuffer sb)
          Implements a terminal append-and-replace step.
(package private)  char charAt(int i)
          Returns this Matcher's input character at index i.
 int end()
          Returns the index of the last character matched, plus one.
 int end(int group)
          Returns the index of the last character, plus one, of the subsequence captured by the given group during the previous match operation.
 boolean find()
          Attempts to find the next subsequence of the input sequence that matches the pattern.
 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.
private  boolean find(int from, int to)
          Initiates a search to find a Pattern within the given bounds.
(package private)  CharSequence getSubSequence(int beginIndex, int endIndex)
          Generates a String from this Matcher's input in the specified range.
(package private)  int getTextLength()
          Returns the end index of the text.
 String group()
          Returns the input subsequence matched by the previous match.
 String group(int group)
          Returns the input subsequence captured by the given group during the previous match operation.
 int groupCount()
          Returns the number of capturing groups in this matcher's pattern.
 boolean lookingAt()
          Attempts to match the input sequence, starting at the beginning, against the pattern.
private  boolean match(int from, int to, int anchor)
          Initiates a search for an anchored match to a Pattern within the given bounds.
 boolean matches()
          Attempts to match the entire input sequence against the pattern.
 Pattern pattern()
          Returns the pattern that is interpreted by this matcher.
 String replaceAll(String replacement)
          Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
 String replaceFirst(String replacement)
          Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
 Matcher reset()
          Resets this matcher.
 Matcher reset(CharSequence input)
          Resets this matcher with a new input sequence.
 int start()
          Returns the start index of the previous match.
 int start(int group)
          Returns the start index of the subsequence captured by the given group during the previous match operation.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

parentPattern

Pattern parentPattern
The Pattern object that created this Matcher.


groups

int[] groups
The storage used by groups. They may contain invalid values if a group was skipped during the matching.


from

int from
The range within the string that is to be matched.


to

int to
The range within the string that is to be matched.


text

CharSequence text
The original string being matched.


ENDANCHOR

static final int ENDANCHOR
Matcher state used by the last node. NOANCHOR is used when a match does not have to consume all of the input. ENDANCHOR is the mode used for matching all the input.

See Also:
Constant Field Values

NOANCHOR

static final int NOANCHOR
See Also:
Constant Field Values

acceptMode

int acceptMode

first

int first
The range of string that last matched the pattern.


last

int last
The range of string that last matched the pattern.


oldLast

int oldLast
The end index of what matched in the last match operation.


lastAppendPosition

int lastAppendPosition
The index of the last position appended in a substitution.


locals

int[] locals
Storage used by nodes to tell what repetition they are on in a pattern, and where groups begin. The nodes themselves are stateless, so they rely on this field to hold state during a match.

Constructor Detail

Matcher

Matcher()
No default constructor.


Matcher

Matcher(Pattern parent,
        CharSequence text)
All matchers have the state used by Pattern during a match.

Method Detail

pattern

public Pattern pattern()
Returns the pattern that is interpreted by this matcher.

Returns:
The pattern for which this matcher was created

reset

public Matcher reset()
Resets this matcher.

Resetting a matcher discards all of its explicit state information and sets its append position to zero.

Returns:
This matcher

reset

public Matcher reset(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.

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

start

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

Returns:
The index of the first character matched
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

start

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().

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 - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

end

public int end()
Returns the index of the last character matched, plus one.

Returns:
The index of the last character matched, plus one
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

end

public int end(int group)
Returns the index of the last character, plus one, 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().

Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
The index of the last character captured by the group, plus one, or -1 if the match was successful but the group itself did not match anything
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

group

public 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.

Returns:
The (possibly empty) subsequence matched by the previous match, in string form
Throws:
IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

group

public 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 emtpy string in the input.

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 - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

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.

Returns:
The number of capturing groups in this matcher's pattern

matches

public boolean matches()
Attempts to match the entire input sequence 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 input sequence matches this matcher's pattern

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 the input sequence 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

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() 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 - If start is less than zero or if start is greater than the length of the input sequence.

lookingAt

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

Like the matches method, this method always starts at the beginning of the input sequence; unlike that method, it does not require that the entire input sequence 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

appendReplacement

public Matcher appendReplacement(StringBuffer sb,
                                 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() - 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().

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(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 and 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 - If no match has yet been attempted, or if the previous match operation failed
IndexOutOfBoundsException - If the replacement string refers to a capturing group that does not exist in the pattern

appendTail

public StringBuffer appendTail(StringBuffer sb)
Implements a terminal append-and-replace step.

This method reads characters from the input sequence, starting at the append position, and appends them to the given string buffer. It is intended to be invoked after one or more invocations of the appendReplacement method in order to copy the remainder of the input sequence.

Parameters:
sb - The target string buffer
Returns:
The target string buffer

replaceAll

public String replaceAll(String replacement)
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.

This method first resets this matcher. It then scans the input sequence looking for matches of the pattern. Characters that are not part of any match are appended directly to the result string; each match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.

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.

Given the regular expression a*b, the input "aabfooaabfooabfoob", and the replacement string "-", an invocation of this method on a matcher for that expression would yield the string "-foo-foo-foo-".

Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

Parameters:
replacement - The replacement string
Returns:
The string constructed by replacing each matching subsequence by the replacement string, substituting captured subsequences as needed

replaceFirst

public String replaceFirst(String replacement)
Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.

This method first resets this matcher. It then scans the input sequence looking for a match of the pattern. Characters that are not part of the match are appended directly to the result string; the match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.

Given the regular expression dog, the input "zzzdogzzzdogzzz", and the replacement string "cat", an invocation of this method on a matcher for that expression would yield the string "zzzcatzzzdogzzz".

Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

Parameters:
replacement - The replacement string
Returns:
The string constructed by replacing the first matching subsequence by the replacement string, substituting captured subsequences as needed

find

private boolean find(int from,
                     int to)
Initiates a search to find a Pattern within the given bounds. The groups are filled with default values and the match of the root of the state machine is called. The state machine will hold the state of the match as it proceeds in this matcher.


match

private boolean match(int from,
                      int to,
                      int anchor)
Initiates a search for an anchored match to a Pattern within the given bounds. The groups are filled with default values and the match of the root of the state machine is called. The state machine will hold the state of the match as it proceeds in this matcher.


getTextLength

int getTextLength()
Returns the end index of the text.

Returns:
the index after the last character in the text

getSubSequence

CharSequence getSubSequence(int beginIndex,
                            int endIndex)
Generates a String from this Matcher's input in the specified range.

Parameters:
beginIndex - the beginning index, inclusive
endIndex - the ending index, exclusive
Returns:
A String generated from this Matcher's input

charAt

char charAt(int i)
Returns this Matcher's input character at index i.

Returns:
A char from the specified index