public final class TMatcher extends Object implements TMatchResult
Matcher
instance has an associated
TPattern
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.Modifier and Type | Method and Description |
---|---|
TMatcher |
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
TPattern in the input. |
boolean |
find(int start)
Returns the next occurrence of the
TPattern 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
TPattern , 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
TPattern against the entire region (or the
entire input, if no region has been set). |
TPattern |
pattern()
Returns the
TPattern 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.
|
TMatcher |
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.
|
TMatcher |
reset()
Resets the
Matcher . |
TMatcher |
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.
|
TMatchResult |
toMatchResult()
Converts the current match into a separate
TMatchResult instance
that is independent from this matcher. |
String |
toString() |
TMatcher |
useAnchoringBounds(boolean value)
Determines whether this matcher has anchoring bounds enabled or not.
|
TMatcher |
usePattern(TPattern pattern)
Sets a new pattern for the
Matcher . |
TMatcher |
useTransparentBounds(boolean value)
Determines whether this matcher has transparent bounds enabled or not.
|
public TMatcher appendReplacement(StringBuffer buffer, String replacement)
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.public TMatcher reset(CharSequence input)
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 TPattern
in the
string will start at the beginning of the input.input
- the new input sequence.Matcher
itself.public TMatcher reset()
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 TPattern
in the string will start at
the beginning of the input.Matcher
itself.public TMatcher region(int start, int end)
start
- the first character of the region.end
- the first character after the end of the region.Matcher
itself.public StringBuffer appendTail(StringBuffer buffer)
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.public String replaceFirst(String replacement)
replacement
- the replacement text.public String replaceAll(String replacement)
replacement
- the replacement text.public TPattern pattern()
TPattern
instance used inside this matcher.Pattern
instance.public String group(int group)
group
in interface TMatchResult
group
- the group, ranging from 0 to groupCount() - 1, with 0
representing the whole pattern.IllegalStateException
- if no successful match has been made.public String group()
group
in interface TMatchResult
IllegalStateException
- if no successful match has been made.public boolean find(int start)
TPattern
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.public boolean find()
TPattern
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.public int start(int group)
start
in interface TMatchResult
group
- the group, ranging from 0 to groupCount() - 1, with 0
representing the whole pattern.IllegalStateException
- if no successful match has been made.public int end(int group)
end
in interface TMatchResult
group
- the group, ranging from 0 to groupCount() - 1, with 0
representing the whole pattern.IllegalStateException
- if no successful match has been made.public boolean matches()
TPattern
against the entire region (or the
entire input, if no region has been set).Pattern
matches the entire
region.public static String quoteReplacement(String s)
s
- the input string.public boolean lookingAt()
TPattern
, 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.public int start()
start
in interface TMatchResult
IllegalStateException
- if no successful match has been made.public int groupCount()
groupCount
in interface TMatchResult
public int end()
end
in interface TMatchResult
IllegalStateException
- if no successful match has been made.public TMatchResult toMatchResult()
TMatchResult
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.public TMatcher useAnchoringBounds(boolean value)
value
- the new value for anchoring bounds.Matcher
itself.public boolean hasAnchoringBounds()
Matcher
uses anchoring bounds.public TMatcher useTransparentBounds(boolean value)
value
- the new value for transparent bounds.Matcher
itself.public boolean hasTransparentBounds()
Matcher
uses anchoring bounds.public int regionStart()
public int regionEnd()
public boolean requireEnd()
public boolean hitEnd()
public TMatcher usePattern(TPattern pattern)
Matcher
. Results of a previous find
get lost. The next attempt to find an occurrence of the TPattern
in the string will start at the beginning of the input.pattern
- the new Pattern
.Matcher
itself.Copyright © 2019. All rights reserved.