Skip to content

Most visited

Recently visited

navigation
Added in API level 1

Matcher

public final class Matcher
extends Object implements MatchResult

java.lang.Object
   ↳ java.util.regex.Matcher


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

Moves to the next occurrence of the pattern in the input.

boolean find(int start)

Returns true if there is another match in the input, starting from the given position.

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

Returns true if this matcher has anchoring bounds enabled.

boolean hasTransparentBounds()

Returns true if this matcher has transparent bounds enabled.

boolean hitEnd()

Returns true if the most recent matching operation attempted to access additional text beyond the available input, meaning that additional input could change the results of the match.

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 index of the first character that is not considered for a match.

int regionStart()

Returns this matcher's region start, that is, the index of 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()

Returns true if the most recent match succeeded and additional input could cause it to fail.

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.

String toString()

Returns a string representing 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()

Invoked when the garbage collector has detected that this instance is no longer reachable.

Inherited methods

From class java.lang.Object
From interface java.util.regex.MatchResult

Public methods

appendReplacement

Added in API level 1
Matcher appendReplacement (StringBuffer buffer, 
                String replacement)

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 StringBuffer: the StringBuffer to append to.
replacement String: the replacement text.
Returns
Matcher the Matcher itself.
Throws
IllegalStateException if no successful match has been made.

appendTail

Added in API level 1
StringBuffer appendTail (StringBuffer buffer)

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 StringBuffer
Returns
StringBuffer the StringBuffer.
Throws
IllegalStateException if no successful match has been made.

end

Added in API level 1
int end ()

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

Returns
int
Throws
IllegalStateException if no successful match has been made.

end

Added in API level 1
int end (int group)

Returns the index of the first character following the text that matched a given group. See group() for an explanation of group indexes.

Parameters
group int
Returns
int
Throws
IllegalStateException if no successful match has been made.

find

Added in API level 1
boolean find ()

Moves to 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
boolean true if (and only if) a match has been found.

find

Added in API level 1
boolean find (int start)

Returns true if there is another match in the input, starting from the given position. The region is ignored.

Parameters
start int
Returns
boolean
Throws
IndexOutOfBoundsException if start < 0 || start > input.length()

group

Added in API level 1
String group ()

Returns the text that matched the whole regular expression.

Returns
String
Throws
IllegalStateException if no successful match has been made.

group

Added in API level 1
String group (int group)

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 int
Returns
String
Throws
IllegalStateException if no successful match has been made.

groupCount

Added in API level 1
int groupCount ()

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

Returns
int

hasAnchoringBounds

Added in API level 1
boolean hasAnchoringBounds ()

Returns true if 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
boolean

hasTransparentBounds

Added in API level 1
boolean hasTransparentBounds ()

Returns true if 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
boolean

hitEnd

Added in API level 1
boolean hitEnd ()

Returns true if the most recent matching operation attempted to access additional text beyond the available input, meaning that additional input could change the results of the match.

Returns
boolean

lookingAt

Added in API level 1
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). Doesn't require the Pattern to match against the whole region.

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

matches

Added in API level 1
boolean matches ()

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

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

pattern

Added in API level 1
Pattern pattern ()

Returns the Pattern instance used inside this matcher.

Returns
Pattern

quoteReplacement

Added in API level 1
String quoteReplacement (String s)

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

Parameters
s String
Returns
String

region

Added in API level 1
Matcher region (int start, 
                int end)

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

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

regionEnd

Added in API level 1
int regionEnd ()

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

Returns
int

regionStart

Added in API level 1
int regionStart ()

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

Returns
int

replaceAll

Added in API level 1
String replaceAll (String replacement)

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

Parameters
replacement String: the replacement text.
Returns
String the modified input string.

replaceFirst

Added in API level 1
String replaceFirst (String replacement)

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

Parameters
replacement String: the replacement text.
Returns
String the modified input string.

requireEnd

Added in API level 1
boolean requireEnd ()

Returns true if the most recent match succeeded and additional input could cause it to fail. If this method returns false and a match was found, then more input might change the match but the match won't be lost. If a match was not found, then requireEnd has no meaning.

Returns
boolean

reset

Added in API level 1
Matcher reset ()

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
Matcher the Matcher itself.

reset

Added in API level 1
Matcher reset (CharSequence input)

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 CharSequence: the new input sequence.
Returns
Matcher the Matcher itself.

start

Added in API level 1
int start ()

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

Returns
int
Throws
IllegalStateException if no successful match has been made.

start

Added in API level 1
int start (int group)

Returns the index of the first character of the text that matched a given group. See group() for an explanation of group indexes.

Parameters
group int
Returns
int
Throws
IllegalStateException if no successful match has been made.

toMatchResult

Added in API level 1
MatchResult toMatchResult ()

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
MatchResult
Throws
IllegalStateException if no successful match has been made.

toString

Added in API level 1
String toString ()

Returns a string representing this Matcher. The format of this string is unspecified.

Returns
String a printable representation of this object.

useAnchoringBounds

Added in API level 1
Matcher useAnchoringBounds (boolean value)

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 boolean
Returns
Matcher the Matcher itself.

usePattern

Added in API level 1
Matcher usePattern (Pattern pattern)

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 Pattern: the new Pattern.
Returns
Matcher the Matcher itself.

useTransparentBounds

Added in API level 1
Matcher useTransparentBounds (boolean value)

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 boolean
Returns
Matcher the Matcher itself.

Protected methods

finalize

Added in API level 1
void finalize ()

Invoked when the garbage collector has detected that this instance is no longer reachable. The default implementation does nothing, but this method can be overridden to free resources.

Note that objects that override finalize are significantly more expensive than objects that don't. Finalizers may be run a long time after the object is no longer reachable, depending on memory pressure, so it's a bad idea to rely on them for cleanup. Note also that finalizers are run on a single VM-wide finalizer thread, so doing blocking work in a finalizer is a bad idea. A finalizer is usually only necessary for a class that has a native peer and needs to call a native method to destroy that peer. Even then, it's better to provide an explicit close method (and implement Closeable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like a BigInteger where typical calling code would have to deal with lots of temporaries. Unfortunately, code that creates lots of temporaries is the worst kind of code from the point of view of the single finalizer thread.

If you must use finalizers, consider at least providing your own ReferenceQueue and having your own thread process that queue.

Unlike constructors, finalizers are not automatically chained. You are responsible for calling super.finalize() yourself.

Uncaught exceptions thrown by finalizers are ignored and do not terminate the finalizer thread. See Effective Java Item 7, "Avoid finalizers" for more.

Throws
Throwable
This site uses cookies to store your preferences for site-specific language and display options.

Hooray!

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.