java.lang.Object | |
↳ | java.util.regex.Matcher |
Provides a means of matching regular expressions against a given input,
finding occurrences of regular expressions in a given input, or replacing
parts of a given input. A Matcher
instance has an associated Pattern
instance and an input text. A typical use case is to
iteratively find all occurrences of the Pattern
, until the end of
the input is reached, as the following example illustrates:
Pattern p = Pattern.compile("[A-Za-z]+"); Matcher m = p.matcher("Hello, Android!"); while (m.find()) { System.out.println(m.group()); // prints "Hello" and "Android" }The
Matcher
has a state that results from the previous operations.
For example, it knows whether the most recent attempt to find the
Pattern
was successful and at which position the next attempt would
resume the search. Depending on the application's needs, it may become
necessary to explicitly reset()
this state from time to time.
Public Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Appends a literal part of the input plus a replacement for the current
match to a given
StringBuffer . | |||||||||||
Appends the (unmatched) remainder of the input to the given
StringBuffer . | |||||||||||
Returns the index of the first character following the text that matched
the whole regular expression.
| |||||||||||
Returns the index of the first character following the text that matched
a given group.
| |||||||||||
Returns the next occurrence of the
Pattern in the input. | |||||||||||
Returns the next occurrence of the
Pattern in the input. | |||||||||||
Returns the text that matched a given group of the regular expression.
| |||||||||||
Returns the text that matched the whole regular expression.
| |||||||||||
Returns the number of groups in the results, which is always equal to
the number of groups in the original regular expression.
| |||||||||||
Indicates whether this matcher has anchoring bounds enabled.
| |||||||||||
Indicates whether this matcher has transparent bounds enabled.
| |||||||||||
Indicates whether the last match hit the end of the input.
| |||||||||||
Tries to match the
Pattern , starting from the beginning of the
region (or the beginning of the input, if no region has been set). | |||||||||||
Tries to match the
Pattern against the entire region (or the
entire input, if no region has been set). | |||||||||||
Returns the
Pattern instance used inside this matcher. | |||||||||||
Returns a replacement string for the given one that has all backslashes
and dollar signs escaped.
| |||||||||||
Resets this matcher and sets a region.
| |||||||||||
Returns this matcher's region end, that is, the first character that is
not considered for a match.
| |||||||||||
Returns this matcher's region start, that is, the first character that is
considered for a match.
| |||||||||||
Replaces all occurrences of this matcher's pattern in the input with a
given string.
| |||||||||||
Replaces the first occurrence of this matcher's pattern in the input with
a given string.
| |||||||||||
Indicates whether more input might change a successful match into an
unsuccessful one.
| |||||||||||
Resets the
Matcher . | |||||||||||
Provides a new input and resets the
Matcher . | |||||||||||
Returns the index of the first character of the text that matched a given
group.
| |||||||||||
Returns the index of the first character of the text that matched the
whole regular expression.
| |||||||||||
Converts the current match into a separate
MatchResult instance
that is independent from this matcher. | |||||||||||
Determines whether this matcher has anchoring bounds enabled or not.
| |||||||||||
Sets a new pattern for the
Matcher . | |||||||||||
Determines whether this matcher has transparent bounds enabled or not.
|
Protected Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Is called before the object's memory is being reclaimed by the VM.
|
[Expand]
Inherited Methods | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
From class java.lang.Object
| |||||||||||
From interface java.util.regex.MatchResult
|
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.
buffer | the StringBuffer to append to. |
---|---|
replacement | the replacement text. |
Matcher
itself.IllegalStateException | if no successful match has been made. |
---|
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.
buffer | the StringBuffer to append to. |
---|
StringBuffer
.IllegalStateException | if no successful match has been made. |
---|
Returns the index of the first character following the text that matched the whole regular expression.
IllegalStateException | if no successful match has been made. |
---|
Returns the index of the first character following the text that matched a given group.
group | the group, ranging from 0 to groupCount() - 1, with 0 representing the whole pattern. |
---|
IllegalStateException | if no successful match has been made. |
---|
Returns the next occurrence of the Pattern
in the input. The
method starts the search from the given character in the input.
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 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 the text that matched a given group of the regular expression.
group | the group, ranging from 0 to groupCount() - 1, with 0 representing the whole pattern. |
---|
IllegalStateException | if no successful match has been made. |
---|
Returns the text that matched the whole regular expression.
IllegalStateException | if no successful match has been made. |
---|
Returns the number of groups in the results, which is always equal to the number of groups in the original regular expression.
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.
Matcher
uses anchoring bounds.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.
Matcher
uses anchoring bounds.Indicates whether the last match hit the end of the input.
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.
Pattern
matches.Tries to match the Pattern
against the entire region (or the
entire input, if no region has been set).
Pattern
matches the entire
region.Returns the Pattern
instance used inside this matcher.
Pattern
instance.Returns a replacement string for the given one that has all backslashes and dollar signs escaped.
s | the input string. |
---|
Resets this matcher and sets a region. Only characters inside the region are considered for a match.
start | the first character of the region. |
---|---|
end | the first character after the end of the region. |
Matcher
itself.Returns this matcher's region end, that is, the first character that is not considered for a match.
Returns this matcher's region start, that is, the first character that is considered for a match.
Replaces all occurrences of this matcher's pattern in the input with a given string.
replacement | the replacement text. |
---|
Replaces the first occurrence of this matcher's pattern in the input with a given string.
replacement | the replacement text. |
---|
Indicates whether more input might change a successful match into an unsuccessful one.
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.
Matcher
itself.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.
input | the new input sequence. |
---|
Matcher
itself.Returns the index of the first character of the text that matched a given group.
group | the group, ranging from 0 to groupCount() - 1, with 0 representing the whole pattern. |
---|
IllegalStateException | if no successful match has been made. |
---|
Returns the index of the first character of the text that matched the whole regular expression.
IllegalStateException | if no successful match has been made. |
---|
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.
MatchResult
.IllegalStateException | if no successful match has been made. |
---|
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.
value | the new value for anchoring bounds. |
---|
Matcher
itself.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.
pattern | the new Pattern . |
---|
Matcher
itself.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.
value | the new value for transparent bounds. |
---|
Matcher
itself.Is called before the object's memory is being reclaimed by the VM. This can only happen once the VM has detected, during a run of the garbage collector, 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.
Throwable |
---|