public final class

Matcher

extends Object
implements MatchResult
java.lang.Object
   ↳ java.util.regex.Matcher

Class Overview

The result of applying a Pattern to a given input. See Pattern for example uses.

Summary

Public Methods
Matcher appendReplacement(StringBuffer buffer, String replacement)
Appends a literal part of the input plus a replacement for the current match to a given StringBuffer.
StringBuffer appendTail(StringBuffer buffer)
Appends the (unmatched) remainder of the input to the given StringBuffer.
int end()
Returns the index of the first character following the text that matched the whole regular expression.
int end(int group)
Returns the index of the first character following the text that matched a given group.
boolean find()
Returns the next occurrence of the Pattern in the input.
boolean find(int start)
Returns the next occurrence of the Pattern in the input.
String group()
Returns the text that matched the whole regular expression.
String group(int group)
Returns the text that matched a given group of the regular expression.
int groupCount()
Returns the number of groups in the results, which is always equal to the number of groups in the original regular expression.
boolean hasAnchoringBounds()
Indicates whether this matcher has anchoring bounds enabled.
boolean hasTransparentBounds()
Indicates whether this matcher has transparent bounds enabled.
boolean hitEnd()
Indicates whether the last match hit the end of the input.
boolean lookingAt()
Tries to match the Pattern, starting from the beginning of the region (or the beginning of the input, if no region has been set).
boolean matches()
Tries to match the Pattern against the entire region (or the entire input, if no region has been set).
Pattern pattern()
Returns the Pattern instance used inside this matcher.
static String quoteReplacement(String s)
Returns a replacement string for the given one that has all backslashes and dollar signs escaped.
Matcher region(int start, int end)
Resets this matcher and sets a region.
int regionEnd()
Returns this matcher's region end, that is, the first character that is not considered for a match.
int regionStart()
Returns this matcher's region start, that is, the first character that is considered for a match.
String replaceAll(String replacement)
Replaces all occurrences of this matcher's pattern in the input with a given string.
String replaceFirst(String replacement)
Replaces the first occurrence of this matcher's pattern in the input with a given string.
boolean requireEnd()
Indicates whether more input might change a successful match into an unsuccessful one.
Matcher reset()
Resets the Matcher.
Matcher reset(CharSequence input)
Provides a new input and resets the Matcher.
int start()
Returns the index of the first character of the text that matched the whole regular expression.
int start(int group)
Returns the index of the first character of the text that matched a given group.
MatchResult toMatchResult()
Converts the current match into a separate MatchResult instance that is independent from this matcher.
Matcher useAnchoringBounds(boolean value)
Determines whether this matcher has anchoring bounds enabled or not.
Matcher usePattern(Pattern pattern)
Sets a new pattern for the Matcher.
Matcher useTransparentBounds(boolean value)
Determines whether this matcher has transparent bounds enabled or not.
Protected Methods
void finalize()
Called before the object's memory is reclaimed by the VM.
[Expand]
Inherited Methods
From class java.lang.Object
From interface java.util.regex.MatchResult

Public Methods

public Matcher appendReplacement (StringBuffer buffer, String replacement)

Since: API Level 1

Appends a literal part of the input plus a replacement for the current match to a given StringBuffer. The literal part is exactly the part of the input between the previous match and the current match. The method can be used in conjunction with find() and appendTail(StringBuffer) to walk through the input and replace all occurrences of the Pattern with something else.

Parameters
buffer the StringBuffer to append to.
replacement the replacement text.
Returns
  • the Matcher itself.
Throws
IllegalStateException if no successful match has been made.

public StringBuffer appendTail (StringBuffer buffer)

Since: API Level 1

Appends the (unmatched) remainder of the input to the given StringBuffer. The method can be used in conjunction with find() and appendReplacement(StringBuffer, String) to walk through the input and replace all matches of the Pattern with something else.

Parameters
buffer the StringBuffer to append to.
Returns
  • the StringBuffer.
Throws
IllegalStateException if no successful match has been made.

public int end ()

Since: API Level 1

Returns the index of the first character following the text that matched the whole regular expression.

Returns
  • the character index.
Throws
IllegalStateException if no successful match has been made.

public int end (int group)

Since: API Level 1

Returns the index of the first character following the text that matched a given group.

Parameters
group the group, ranging from 0 to groupCount() - 1, with 0 representing the whole pattern.
Returns
  • the character index.
Throws
IllegalStateException if no successful match has been made.

public boolean find ()

Since: API Level 1

Returns the next occurrence of the Pattern in the input. If a previous match was successful, the method continues the search from the first character following that match in the input. Otherwise it searches either from the region start (if one has been set), or from position 0.

Returns
  • true if (and only if) a match has been found.

public boolean find (int start)

Since: API Level 1

Returns the next occurrence of the Pattern in the input. The method starts the search from the given character in the input.

Parameters
start The index in the input at which the find operation is to begin. If this is less than the start of the region, it is automatically adjusted to that value. If it is beyond the end of the region, the method will fail.
Returns
  • true if (and only if) a match has been found.

public String group ()

Since: API Level 1

Returns the text that matched the whole regular expression.

Returns
  • the text.
Throws
IllegalStateException if no successful match has been made.

public String group (int group)

Since: API Level 1

Returns the text that matched a given group of the regular expression. Explicit capturing groups in the pattern are numbered left to right in order of their opening parenthesis, starting at 1. The special group 0 represents the entire match (as if the entire pattern is surrounded by an implicit capturing group). For example, "a((b)c)" matching "abc" would give the following groups:

 0 "abc"
 1 "bc"
 2 "b"
 

An optional capturing group that failed to match as part of an overall successful match (for example, "a(b)?c" matching "ac") returns null. A capturing group that matched the empty string (for example, "a(b?)c" matching "ac") returns the empty string.

Parameters
group the group, ranging from 0 to groupCount() - 1, with 0 representing the whole pattern.
Returns
  • the text that matched the group.
Throws
IllegalStateException if no successful match has been made.

public int groupCount ()

Since: API Level 1

Returns the number of groups in the results, which is always equal to the number of groups in the original regular expression.

Returns
  • the number of groups.

public boolean hasAnchoringBounds ()

Since: API Level 1

Indicates whether this matcher has anchoring bounds enabled. When anchoring bounds are enabled, the start and end of the input match the '^' and '$' meta-characters, otherwise not. Anchoring bounds are enabled by default.

Returns
  • true if (and only if) the Matcher uses anchoring bounds.

public boolean hasTransparentBounds ()

Since: API Level 1

Indicates whether this matcher has transparent bounds enabled. When transparent bounds are enabled, the parts of the input outside the region are subject to lookahead and lookbehind, otherwise they are not. Transparent bounds are disabled by default.

Returns
  • true if (and only if) the Matcher uses anchoring bounds.

public boolean hitEnd ()

Since: API Level 1

Indicates whether the last match hit the end of the input.

Returns
  • true if (and only if) the last match hit the end of the input.

public boolean lookingAt ()

Since: API Level 1

Tries to match the Pattern, starting from the beginning of the region (or the beginning of the input, if no region has been set). Doesn't require the Pattern to match against the whole region.

Returns
  • true if (and only if) the Pattern matches.

public boolean matches ()

Since: API Level 1

Tries to match the Pattern against the entire region (or the entire input, if no region has been set).

Returns
  • true if (and only if) the Pattern matches the entire region.

public Pattern pattern ()

Since: API Level 1

Returns the Pattern instance used inside this matcher.

Returns
  • the Pattern instance.

public static String quoteReplacement (String s)

Since: API Level 1

Returns a replacement string for the given one that has all backslashes and dollar signs escaped.

Parameters
s the input string.
Returns
  • the input string, with all backslashes and dollar signs having been escaped.

public Matcher region (int start, int end)

Since: API Level 1

Resets this matcher and sets a region. Only characters inside the region are considered for a match.

Parameters
start the first character of the region.
end the first character after the end of the region.
Returns
  • the Matcher itself.

public int regionEnd ()

Since: API Level 1

Returns this matcher's region end, that is, the first character that is not considered for a match.

Returns
  • the end of the region.

public int regionStart ()

Since: API Level 1

Returns this matcher's region start, that is, the first character that is considered for a match.

Returns
  • the start of the region.

public String replaceAll (String replacement)

Since: API Level 1

Replaces all occurrences of this matcher's pattern in the input with a given string.

Parameters
replacement the replacement text.
Returns
  • the modified input string.

public String replaceFirst (String replacement)

Since: API Level 1

Replaces the first occurrence of this matcher's pattern in the input with a given string.

Parameters
replacement the replacement text.
Returns
  • the modified input string.

public boolean requireEnd ()

Since: API Level 1

Indicates whether more input might change a successful match into an unsuccessful one.

Returns
  • true if (and only if) more input might change a successful match into an unsuccessful one.

public Matcher reset ()

Since: API Level 1

Resets the Matcher. This results in the region being set to the whole input. Results of a previous find get lost. The next attempt to find an occurrence of the Pattern in the string will start at the beginning of the input.

Returns
  • the Matcher itself.

public Matcher reset (CharSequence input)

Since: API Level 1

Provides a new input and resets the Matcher. This results in the region being set to the whole input. Results of a previous find get lost. The next attempt to find an occurrence of the Pattern in the string will start at the beginning of the input.

Parameters
input the new input sequence.
Returns
  • the Matcher itself.

public int start ()

Since: API Level 1

Returns the index of the first character of the text that matched the whole regular expression.

Returns
  • the character index.
Throws
IllegalStateException if no successful match has been made.

public int start (int group)

Since: API Level 1

Returns the index of the first character of the text that matched a given group.

Parameters
group the group, ranging from 0 to groupCount() - 1, with 0 representing the whole pattern.
Returns
  • the character index.
Throws
IllegalStateException if no successful match has been made.

public MatchResult toMatchResult ()

Since: API Level 1

Converts the current match into a separate MatchResult instance that is independent from this matcher. The new object is unaffected when the state of this matcher changes.

Returns
  • the new MatchResult.
Throws
IllegalStateException if no successful match has been made.

public Matcher useAnchoringBounds (boolean value)

Since: API Level 1

Determines whether this matcher has anchoring bounds enabled or not. When anchoring bounds are enabled, the start and end of the input match the '^' and '$' meta-characters, otherwise not. Anchoring bounds are enabled by default.

Parameters
value the new value for anchoring bounds.
Returns
  • the Matcher itself.

public Matcher usePattern (Pattern pattern)

Since: API Level 1

Sets a new pattern for the Matcher. Results of a previous find get lost. The next attempt to find an occurrence of the Pattern in the string will start at the beginning of the input.

Parameters
pattern the new Pattern.
Returns
  • the Matcher itself.

public Matcher useTransparentBounds (boolean value)

Since: API Level 1

Determines whether this matcher has transparent bounds enabled or not. When transparent bounds are enabled, the parts of the input outside the region are subject to lookahead and lookbehind, otherwise they are not. Transparent bounds are disabled by default.

Parameters
value the new value for transparent bounds.
Returns
  • the Matcher itself.

Protected Methods

protected void finalize ()

Since: API Level 1

Called before the object's memory is reclaimed by the VM. This can only happen once the garbage collector has detected that the object is no longer reachable by any thread of the running application.

The method can be used to free system resources or perform other cleanup before the object is garbage collected. The default implementation of the method is empty, which is also expected by the VM, but subclasses can override finalize() as required. Uncaught exceptions which are thrown during the execution of this method cause it to terminate immediately but are otherwise ignored.

Note that the VM does guarantee that finalize() is called at most once for any object, but it doesn't guarantee when (if at all) finalize() will be called. For example, object B's finalize() can delay the execution of object A's finalize() method and therefore it can delay the reclamation of A's memory. To be safe, use a ReferenceQueue, because it provides more control over the way the VM deals with references during garbage collection.

Throws
Throwable