Class TMatcher
java.lang.Object
org.teavm.classlib.java.util.regex.TMatcher
- All Implemented Interfaces:
TMatchResult
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
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.-
Method Summary
Modifier and TypeMethodDescriptionappendReplacement(StringBuffer buffer, String replacement)
Appends a literal part of the input plus a replacement for the current match to a givenStringBuffer
.appendTail(StringBuffer buffer)
Appends the (unmatched) remainder of the input to the givenStringBuffer
.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 theTPattern
in the input.boolean
find(int start)
Returns the next occurrence of theTPattern
in the input.group()
Returns the text that matched the whole regular expression.group(int group)
Returns the text that matched a given group of the regular expression.int
Returns the number of groups in the results, which is always equal to the number of groups in the original regular expression.boolean
Indicates whether this matcher has anchoring bounds enabled.boolean
Indicates whether this matcher has transparent bounds enabled.boolean
hitEnd()
Indicates whether the last match hit the end of the input.boolean
Tries to match theTPattern
, starting from the beginning of the region (or the beginning of the input, if no region has been set).boolean
matches()
Tries to match theTPattern
against the entire region (or the entire input, if no region has been set).pattern()
Returns theTPattern
instance used inside this matcher.static String
Returns a replacement string for the given one that has all backslashes and dollar signs escaped.region(int start, int end)
Resets this matcher and sets a region.int
Returns this matcher's region end, that is, the first character that is not considered for a match.int
Returns this matcher's region start, that is, the first character that is considered for a match.replaceAll(String replacement)
Replaces all occurrences of this matcher's pattern in the input with a given string.replaceFirst(String replacement)
Replaces the first occurrence of this matcher's pattern in the input with a given string.boolean
Indicates whether more input might change a successful match into an unsuccessful one.reset()
Resets theMatcher
.reset(CharSequence input)
Provides a new input and resets theMatcher
.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.Converts the current match into a separateTMatchResult
instance that is independent from this matcher.toString()
useAnchoringBounds(boolean value)
Determines whether this matcher has anchoring bounds enabled or not.usePattern(TPattern pattern)
Sets a new pattern for theMatcher
.useTransparentBounds(boolean value)
Determines whether this matcher has transparent bounds enabled or not.
-
Method Details
-
appendReplacement
Appends a literal part of the input plus a replacement for the current match to a givenStringBuffer
. 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 withfind()
andappendTail(StringBuffer)
to walk through the input and replace all occurrences of thePattern
with something else.- Parameters:
buffer
- theStringBuffer
to append to.replacement
- the replacement text.- Returns:
- the
Matcher
itself. - Throws:
IllegalStateException
- if no successful match has been made.
-
reset
Provides a new input and resets theMatcher
. 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 theTPattern
in the string will start at the beginning of the input.- Parameters:
input
- the new input sequence.- Returns:
- the
Matcher
itself.
-
reset
Resets theMatcher
. 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 theTPattern
in the string will start at the beginning of the input.- Returns:
- the
Matcher
itself.
-
region
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.
-
appendTail
Appends the (unmatched) remainder of the input to the givenStringBuffer
. The method can be used in conjunction withfind()
andappendReplacement(StringBuffer, String)
to walk through the input and replace all matches of thePattern
with something else.- Parameters:
buffer
- theStringBuffer
to append to.- Returns:
- the
StringBuffer
. - Throws:
IllegalStateException
- if no successful match has been made.
-
replaceFirst
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.
-
replaceAll
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.
-
pattern
Returns theTPattern
instance used inside this matcher.- Returns:
- the
Pattern
instance.
-
group
Returns the text that matched a given group of the regular expression.- Specified by:
group
in interfaceTMatchResult
- 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.
-
group
Returns the text that matched the whole regular expression.- Specified by:
group
in interfaceTMatchResult
- Returns:
- the text.
- Throws:
IllegalStateException
- if no successful match has been made.
-
find
public boolean find(int start)Returns the next occurrence of theTPattern
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.
-
find
public boolean find()Returns the next occurrence of theTPattern
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.
-
start
public int start(int group)Returns the index of the first character of the text that matched a given group.- Specified by:
start
in interfaceTMatchResult
- 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.
-
end
public int end(int group)Returns the index of the first character following the text that matched a given group.- Specified by:
end
in interfaceTMatchResult
- 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.
-
matches
public boolean matches()Tries to match theTPattern
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.
-
quoteReplacement
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.
-
lookingAt
public boolean lookingAt()Tries to match theTPattern
, starting from the beginning of the region (or the beginning of the input, if no region has been set). Doesn't require thePattern
to match against the whole region.- Returns:
- true if (and only if) the
Pattern
matches.
-
start
public int start()Returns the index of the first character of the text that matched the whole regular expression.- Specified by:
start
in interfaceTMatchResult
- Returns:
- the character index.
- Throws:
IllegalStateException
- if no successful match has been made.
-
groupCount
public int groupCount()Returns the number of groups in the results, which is always equal to the number of groups in the original regular expression.- Specified by:
groupCount
in interfaceTMatchResult
- Returns:
- the number of groups.
-
end
public int end()Returns the index of the first character following the text that matched the whole regular expression.- Specified by:
end
in interfaceTMatchResult
- Returns:
- the character index.
- Throws:
IllegalStateException
- if no successful match has been made.
-
toMatchResult
Converts the current match into a separateTMatchResult
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.
-
useAnchoringBounds
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.
-
hasAnchoringBounds
public boolean hasAnchoringBounds()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.
-
useTransparentBounds
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.
-
hasTransparentBounds
public boolean hasTransparentBounds()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.
-
regionStart
public int regionStart()Returns this matcher's region start, that is, the first character that is considered for a match.- Returns:
- the start of the region.
-
regionEnd
public int regionEnd()Returns this matcher's region end, that is, the first character that is not considered for a match.- Returns:
- the end of the region.
-
requireEnd
public boolean requireEnd()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.
-
hitEnd
public boolean hitEnd()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.
-
usePattern
Sets a new pattern for theMatcher
. Results of a previous find get lost. The next attempt to find an occurrence of theTPattern
in the string will start at the beginning of the input.- Parameters:
pattern
- the newPattern
.- Returns:
- the
Matcher
itself.
-
toString
-