Class Matcher
- All Implemented Interfaces:
MatchResult
public final class Matcher extends Object implements MatchResult
Pattern to a given input. See Pattern for
example uses.-
Method Summary
Modifier and Type Method Description MatcherappendReplacement(StringBuffer buffer, String replacement)Appends a literal part of the input plus a replacement for the current match to a givenStringBuffer.StringBufferappendTail(StringBuffer buffer)Appends the (unmatched) remainder of the input to the givenStringBuffer.intend()Returns the index of the first character following the text that matched the whole regular expression.intend(int group)Returns the index of the first character following the text that matched a given group.protected voidfinalize()Invoked when the garbage collector has detected that this instance is no longer reachable.booleanfind()Returns the next occurrence of thePatternin the input.booleanfind(int start)Returns true if there is another match in the input, starting from the given position.Stringgroup()Returns the text that matched the whole regular expression.Stringgroup(int group)Returns the text that matched a given group of the regular expression.intgroupCount()Returns the number of groups in the results, which is always equal to the number of groups in the original regular expression.booleanhasAnchoringBounds()Returns true if this matcher has anchoring bounds enabled.booleanhasTransparentBounds()Returns true if this matcher has transparent bounds enabled.booleanhitEnd()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.booleanlookingAt()Tries to match thePattern, starting from the beginning of the region (or the beginning of the input, if no region has been set).booleanmatches()Tries to match thePatternagainst the entire region (or the entire input, if no region has been set).Patternpattern()Returns thePatterninstance used inside this matcher.static StringquoteReplacement(String s)Returns a replacement string for the given one that has all backslashes and dollar signs escaped.Matcherregion(int start, int end)Resets this matcher and sets a region.intregionEnd()Returns this matcher's region end, that is, the index of the first character that is not considered for a match.intregionStart()Returns this matcher's region start, that is, the index of the first character that is considered for a match.StringreplaceAll(String replacement)Replaces all occurrences of this matcher's pattern in the input with a given string.StringreplaceFirst(String replacement)Replaces the first occurrence of this matcher's pattern in the input with a given string.booleanrequireEnd()Returns true if the most recent match succeeded and additional input could cause it to fail.Matcherreset()Resets theMatcher.Matcherreset(CharSequence input)Provides a new input and resets theMatcher.intstart()Returns the index of the first character of the text that matched the whole regular expression.intstart(int group)Returns the index of the first character of the text that matched a given group.MatchResulttoMatchResult()Converts the current match into a separateMatchResultinstance that is independent from this matcher.StringtoString()Returns a string representing thisMatcher.MatcheruseAnchoringBounds(boolean value)Determines whether this matcher has anchoring bounds enabled or not.MatcherusePattern(Pattern pattern)Sets a new pattern for theMatcher.MatcheruseTransparentBounds(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 thePatternwith something else.- Parameters:
buffer- theStringBufferto append to.replacement- the replacement text.- Returns:
- the
Matcheritself. - Throws:
IllegalStateException- if no successful match has been made.
-
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 thePatternin the string will start at the beginning of the input.- Returns:
- the
Matcheritself.
-
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 thePatternin the string will start at the beginning of the input.- Parameters:
input- the new input sequence.- Returns:
- the
Matcheritself.
-
usePattern
Sets a new pattern for theMatcher. Results of a previous find get lost. The next attempt to find an occurrence of thePatternin the string will start at the beginning of the input.- Parameters:
pattern- the newPattern.- Returns:
- the
Matcheritself.
-
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
Matcheritself.
-
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 thePatternwith something else.- Parameters:
buffer- theStringBufferto 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 thePatterninstance used inside this matcher.- Returns:
- the
Patterninstance.
-
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.
- Specified by:
groupin interfaceMatchResult- 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:
groupin interfaceMatchResult- Returns:
- the text.
- Throws:
IllegalStateException- if no successful match has been made.
-
find
public boolean find(int start)Returns true if there is another match in the input, starting from the given position. The region is ignored.- Throws:
IndexOutOfBoundsException- ifstart < 0 || start > input.length()
-
find
public boolean find()Returns the next occurrence of thePatternin 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.
-
lookingAt
public boolean lookingAt()Tries to match thePattern, starting from the beginning of the region (or the beginning of the input, if no region has been set). Doesn't require thePatternto match against the whole region.- Returns:
- true if (and only if) the
Patternmatches.
-
matches
public boolean matches()Tries to match thePatternagainst the entire region (or the entire input, if no region has been set).- Returns:
- true if (and only if) the
Patternmatches the entire region.
-
start
Returns the index of the first character of the text that matched a given group.- Specified by:
startin interfaceMatchResult- 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:
endin interfaceMatchResult- 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.
-
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.
-
start
public int start()Returns the index of the first character of the text that matched the whole regular expression.- Specified by:
startin interfaceMatchResult- 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:
groupCountin interfaceMatchResult- 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:
endin interfaceMatchResult- Returns:
- the character index.
- Throws:
IllegalStateException- if no successful match has been made.
-
toMatchResult
Converts the current match into a separateMatchResultinstance 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
Matcheritself.
-
hasAnchoringBounds
public 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. -
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
Matcheritself.
-
hasTransparentBounds
public 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. -
regionStart
public int regionStart()Returns this matcher's region start, that is, the index of the first character that is considered for a match. -
regionEnd
public int regionEnd()Returns this matcher's region end, that is, the index of the first character that is not considered for a match. -
requireEnd
public 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. -
hitEnd
public 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. -
finalize
Description copied from class:ObjectInvoked 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
finalizeare 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 explicitclosemethod (and implementCloseable), and insist that callers manually dispose of instances. This works well for something like files, but less well for something like aBigIntegerwhere 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
ReferenceQueueand 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.
-
toString
Returns a string representing thisMatcher. The format of this string is unspecified.
-