info.codesaway.util.regex
Class Matcher

java.lang.Object
  extended by info.codesaway.util.regex.Matcher
All Implemented Interfaces:
MatchResult, java.lang.Cloneable, java.lang.Iterable<MatchResult>

public final class Matcher
extends java.lang.Object
implements MatchResult, java.lang.Cloneable

An engine that performs match operations on a character sequence by interpreting a Pattern.

This class is an extension of Java's Matcher class. Javadocs were copied and appended with the added functionality.

A matcher is created from a pattern by invoking the pattern's matcher method. Once created, a matcher can be used to perform three different kinds of match operations:

Each of these methods returns a boolean indicating success or failure. More information about a successful match can be obtained by querying the state of the matcher.

A matcher finds matches in a subset of its input called the region. By default, the region contains all of the matcher's input. The region can be modified via theregion method and queried via the regionStart and regionEnd methods. The way that the region boundaries interact with some pattern constructs can be changed. See useAnchoringBounds and useTransparentBounds for more details.

This class also defines methods for replacing matched subsequences with new strings whose contents can, if desired, be computed from the match result. The appendReplacement and appendTail methods can be used in tandem in order to collect the result into an existing string buffer, or the more convenient replaceAll method can be used to create a string in which every matching subsequence in the input sequence is replaced.

The explicit state of a matcher includes the start and end indices of the most recent successful match. It also includes the start and end indices of the input subsequence captured by each capturing group in the pattern as well as a total count of such subsequences. As a convenience, methods are also provided for returning these captured subsequences in string form.

The explicit state of a matcher is initially undefined; attempting to query any part of it before a successful match will cause an IllegalStateException to be thrown. The explicit state of a matcher is recomputed by every match operation.

The implicit state of a matcher includes the input character sequence as well as the append position, which is initially zero and is updated by the appendReplacement method.

A matcher may be reset explicitly by invoking its reset() method or, if a new input sequence is desired, its reset(CharSequence) method. Resetting a matcher discards its explicit state information and sets the append position to zero.

Instances of this class are not safe for use by multiple concurrent threads.


Constructor Summary
Matcher(java.util.regex.Matcher matcher)
           
 
Method Summary
 Matcher appendReplacement(java.lang.StringBuffer sb, java.lang.String replacement)
          Implements a non-terminal append-and-replace step.
 java.lang.StringBuffer appendTail(java.lang.StringBuffer sb)
          Implements a terminal append-and-replace step.
 boolean asBoolean()
          Alias for find().
 Matcher clone()
           
 boolean containsKey(java.lang.Object key)
           
 boolean containsValue(java.lang.Object value)
           
 int end()
          Returns the offset after the last character matched.
 int end(int group)
          Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.
 int end(java.lang.String group)
          Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.
 int end(java.lang.String groupName, int occurrence)
          Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.
 java.util.Set<java.util.Map.Entry<java.lang.Integer,java.lang.String>> entrySet()
           
 boolean find()
          Attempts to find the next subsequence of the input sequence that matches the pattern.
 boolean find(int start)
          Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.
 java.lang.String get(java.lang.Object key)
           
 java.util.List getAt(java.util.Collection<?> indexes)
           
 java.lang.Object getAt(int idx)
          Alias for MatchResult.group(int)
 java.lang.String getAt(java.lang.String group)
          Alias for MatchResult.group(String)
 java.util.Map.Entry<java.lang.Integer,java.lang.String> getEntry(int group)
           
 java.lang.String getGroupName(int group)
          Returns the group name (if any) for the specified group.
 java.util.List<MatchResult> getResults()
           
 java.lang.String group()
          Returns the input subsequence matched by the previous match.
 java.lang.String group(int group)
          Returns the input subsequence captured by the given group during the previous match operation.
 java.lang.String group(int group, java.lang.String defaultValue)
          Returns the input subsequence captured by the given group during the previous match operation.
 java.lang.String group(java.lang.String group)
          Returns the input subsequence captured by the given group during the previous match operation.
 java.lang.String group(java.lang.String groupName, int occurrence)
          Returns the input subsequence captured by the given group during the previous match operation.
 java.lang.String group(java.lang.String groupName, int occurrence, java.lang.String defaultValue)
          Returns the input subsequence captured by the given group during the previous match operation.
 java.lang.String group(java.lang.String group, java.lang.String defaultValue)
          Returns the input subsequence captured by the given group during the previous match operation.
 int groupCount()
          Returns the number of capturing groups in this matcher's pattern.
 int groupCount(int group)
           
 int groupCount(java.lang.String groupName)
          Returns the number of capturing groups (with the given group name) in this matcher's pattern.
 boolean hasAnchoringBounds()
          Queries the anchoring of region bounds for this matcher.
 boolean hasTransparentBounds()
          Queries the transparency of region bounds for this matcher.
 boolean hitEnd()
          Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher.
 boolean isEmpty()
          Returns whether the previous match matched the empty string.
 boolean isEmpty(int group)
          Returns whether the specified group matched the empty string.
 boolean isEmpty(java.lang.String group)
          Returns whether the specified group matched the empty string.
 boolean isEmpty(java.lang.String groupName, int occurrence)
          Returns whether the specified group matched the empty string.
 boolean isMatchResult()
           
 java.util.Iterator<MatchResult> iterator()
          Returns an Iterator which traverses each match.
 java.util.Set<java.lang.Integer> keySet()
           
 boolean lookingAt()
          Attempts to match the input sequence, starting at the beginning of the region, against the pattern.
 boolean matched()
          Returns whether the match was successful.
 boolean matched(int group)
          Returns whether the specified group matched any part of the input sequence.
 boolean matched(java.lang.String group)
          Returns whether the specified group matched any part of the input sequence.
 boolean matched(java.lang.String groupName, int occurrence)
          Returns whether the specified group matched any part of the input sequence.
 boolean matches()
          Attempts to match the entire region against the pattern.
 MatchResult next()
           
 int occurrence(int groupIndex)
          Returns the occurrence of the first matched group with the given index.
 int occurrence(java.lang.String groupName)
          Returns the occurrence of the first matched group with the given name.
 Pattern pattern()
          Returns the pattern that is interpreted by this matcher.
static java.lang.String quoteReplacement(java.lang.String s)
          Returns a literal replacement String for the specified String.
 Matcher region(int start, int end)
          Sets the limits of this matcher's region.
 int regionEnd()
          Reports the end index (exclusive) of this matcher's region.
 int regionStart()
          Reports the start index of this matcher's region.
 java.lang.String replaceAll(java.lang.String replacement)
          Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.
 java.lang.String replaceFirst(java.lang.String replacement)
          Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.
 boolean requireEnd()
          Returns true if more input could change a positive match into a negative one.
 Matcher reset()
          Resets this matcher.
 Matcher reset(java.lang.CharSequence input)
          Resets this matcher with a new input sequence.
 int size()
           
 int start()
          Returns the start index of the previous match.
 int start(int group)
          Returns the start index of the subsequence captured by the given group during the previous match operation.
 int start(java.lang.String group)
          Returns the start index of the subsequence captured by the given group during the previous match operation.
 int start(java.lang.String groupName, int occurrence)
          Returns the start index of the subsequence captured by the given group during the previous match operation.
 java.lang.String text()
          Returns the string being matched.
 MatchResult toMatchResult()
          Returns the match state of this matcher as a MatchResult.
 java.lang.String toString()
          Returns the string representation of this matcher.
 boolean treatNullAsEmptyString()
           
 Matcher treatNullAsEmptyString(boolean treatNullAsEmptyString)
           
 Matcher useAnchoringBounds(boolean b)
          Sets the anchoring of region bounds for this matcher.
 Matcher usePattern(Pattern newPattern)
          Changes the Pattern that this Matcher uses to find matches with.
 Matcher useTransparentBounds(boolean b)
          Sets the transparency of region bounds for this matcher.
 java.util.List<java.lang.String> values()
           
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

Matcher

public Matcher(java.util.regex.Matcher matcher)
Parameters:
matcher -
Since:
0.2
Method Detail

pattern

public Pattern pattern()
Returns the pattern that is interpreted by this matcher.

Specified by:
pattern in interface MatchResult
Returns:
The pattern for which this matcher was created

toMatchResult

public MatchResult toMatchResult()
Returns the match state of this matcher as a MatchResult. The result is unaffected by subsequent operations performed upon this matcher.

Returns:
a MatchResult with the state of this matcher

clone

public Matcher clone()
Overrides:
clone in class java.lang.Object

isMatchResult

public boolean isMatchResult()
Returns:
Since:
0.2

usePattern

public Matcher usePattern(Pattern newPattern)
Changes the Pattern that this Matcher uses to find matches with.

This method causes this matcher to lose information about the groups of the last match that occurred. The matcher's position in the input is maintained and its last append position is unaffected.

Parameters:
newPattern - The new pattern used by this matcher
Returns:
This matcher
Throws:
java.lang.IllegalArgumentException - If newPattern is null

reset

public Matcher reset()
Resets this matcher.

Resetting a matcher discards all of its explicit state information and sets its append position to zero. The matcher's region is set to the default region, which is its entire character sequence. The anchoring and transparency of this matcher's region boundaries are unaffected.

Returns:
This matcher

reset

public Matcher reset(java.lang.CharSequence input)
Resets this matcher with a new input sequence.

Resetting a matcher discards all of its explicit state information and sets its append position to zero. The matcher's region is set to the default region, which is its entire character sequence. The anchoring and transparency of this matcher's region boundaries are unaffected.

Parameters:
input - The new input character sequence
Returns:
This matcher

start

public int start()
Returns the start index of the previous match.

Specified by:
start in interface MatchResult
Returns:
The index of the first character matched
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

start

public int start(int group)
Returns the start index of the subsequence captured by the given group during the previous match operation.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.start(0) is equivalent to m.start().

Specified by:
start in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
The index of the first character captured by the group, or -1 if the match was successful but the group itself did not match anything
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

start

public int start(java.lang.String group)
Returns the start index of the subsequence captured by the given group during the previous match operation.

Specified by:
start in interface MatchResult
Parameters:
group - A capturing group in this matcher's pattern
Returns:
The index of the first character captured by the group, or -1 if the match was successful but the group itself did not match anything
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

start

public int start(java.lang.String groupName,
                 int occurrence)
Returns the start index of the subsequence captured by the given group during the previous match operation.

An invocation of this convenience method of the form

 m.start(groupName, occurrence)

behaves in exactly the same way as

 m.start(groupName + "[" + occurrence + "]")

Specified by:
start in interface MatchResult
Parameters:
groupName - The group name for a capturing group in this matcher's pattern
occurrence - The occurrence of the specified group name
Returns:
The index of the first character captured by the group, or -1 if the match was successful but the group itself did not match anything
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

end

public int end()
Returns the offset after the last character matched.

Specified by:
end in interface MatchResult
Returns:
The offset after the last character matched
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

end

public int end(int group)
Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.end(0) is equivalent to m.end().

Specified by:
end in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
The offset after the last character captured by the group, or -1 if the match was successful but the group itself did not match anything
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

end

public int end(java.lang.String group)
Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.

Specified by:
end in interface MatchResult
Parameters:
group - A capturing group in this matcher's pattern
Returns:
The offset after the last character captured by the group, or -1 if the match was successful but the group itself did not match anything
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

end

public int end(java.lang.String groupName,
               int occurrence)
Returns the offset after the last character of the subsequence captured by the given group during the previous match operation.

An invocation of this convenience method of the form

 m.end(groupName, occurrence)

behaves in exactly the same way as

 m.end(groupName + "[" + occurrence + "]")

Specified by:
end in interface MatchResult
Parameters:
groupName - The group name for a capturing group in this matcher's pattern
occurrence - The occurrence of the specified group name
Returns:
The offset after the last character captured by the group, or -1 if the match was successful but the group itself did not match anything
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

occurrence

public int occurrence(int groupIndex)
Returns the occurrence of the first matched group with the given index.

Branch reset patterns allow multiple capture groups with the same group index to exist. This method offers a way to determine which occurrence matched.

Specified by:
occurrence in interface MatchResult
Parameters:
groupIndex - the name of the group
Returns:
the occurrence for the first matched group with the given index


occurrence

public int occurrence(java.lang.String groupName)
Returns the occurrence of the first matched group with the given name.

Branch reset patterns and the Pattern.DUPLICATE_NAMES flag allow multiple capture groups with the same group name to exist. This method offers a way to determine which occurrence matched.

Specified by:
occurrence in interface MatchResult
Parameters:
groupName - the name of the group
Returns:
the occurrence of the first matched group with the given name.


group

public java.lang.String group()
Returns the input subsequence matched by the previous match.

For a matcher m with input sequence s, the expressions m.group() and s.substring(m. start(), m.end()) are equivalent.

Note that some patterns, for example a*, match the empty string. This method will return the empty string when the pattern successfully matches the empty string in the input.

Specified by:
group in interface MatchResult
Returns:
The (possibly empty) subsequence matched by the previous match, in string form
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

group

public java.lang.String group(int group)
Returns the input subsequence captured by the given group during the previous match operation.

For a matcher m, input sequence s, and group index g, the expressions m.group(g) and s.substring(m.start(g),  m.end(g)) are equivalent.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.group(0) is equivalent to m.group().

If the match was successful but the group specified failed to match any part of the input sequence, then null is returned. Note that some groups, for example (a*), match the empty string. This method will return the empty string when such a group successfully matches the empty string in the input.

Specified by:
group in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
The (possibly empty) subsequence captured by the group during the previous match, or null if the group failed to match part of the input
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

group

public java.lang.String group(int group,
                              java.lang.String defaultValue)
Returns the input subsequence captured by the given group during the previous match operation.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.group(0, null) is equivalent to m.group().

If the match was successful but the group specified failed to match any part of the input sequence, then defaultValue is returned, whereas group(int) would return null. Otherwise, the return is equivalent to that of m.group(group).

As a note,

m.group(group, null)

behaves in exactly the same way as

m.group(group)

Specified by:
group in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
defaultValue - The string to return if group(int) would return null
Returns:
The (possibly empty) subsequence captured by the group during the previous match, or defaultValue if the group failed to match part of the input
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index
See Also:
group(int)

group

public java.lang.String group(java.lang.String group)
Returns the input subsequence captured by the given group during the previous match operation.

If the match was successful but the group specified failed to match any part of the input sequence, then null is returned. Note that some groups, for example (a*), match the empty string. This method will return the empty string when such a group successfully matches the empty string in the input.

Specified by:
group in interface MatchResult
Parameters:
group - A capturing group in this matcher's pattern
Returns:
The (possibly empty) subsequence captured by the group during the previous match, or null if the group failed to match part of the input
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

group

public java.lang.String group(java.lang.String group,
                              java.lang.String defaultValue)
Returns the input subsequence captured by the given group during the previous match operation.

If the match was successful but the group specified failed to match any part of the input sequence, then defaultValue is returned, whereas group(String) would return null. Otherwise, the return is equivalent to that of m.group(group).

As a note,

m.group(group, null)

behaves in exactly the same way as

m.group(group)

Specified by:
group in interface MatchResult
Parameters:
group - A capturing group in this matcher's pattern
defaultValue - The string to return if group(String) would return null
Returns:
The (possibly empty) subsequence captured by the group during the previous match, or defaultValue if the group failed to match part of the input
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group
See Also:
group(String)

group

public java.lang.String group(java.lang.String groupName,
                              int occurrence)
Returns the input subsequence captured by the given group during the previous match operation.

If the match was successful but the group specified failed to match any part of the input sequence, then null is returned. Note that some groups, for example (a*), match the empty string. This method will return the empty string when such a group successfully matches the empty string in the input.

An invocation of this convenience method of the form

m.group(groupName, occurrence)

behaves in exactly the same way as

 m.group(groupName + "[" + occurrence + "]")

Specified by:
group in interface MatchResult
Parameters:
groupName - The group name for a capturing group in this matcher's pattern
occurrence - The occurrence of the specified group name
Returns:
The (possibly empty) subsequence captured by the group during the previous match, or null if the group failed to match part of the input
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

group

public java.lang.String group(java.lang.String groupName,
                              int occurrence,
                              java.lang.String defaultValue)
Returns the input subsequence captured by the given group during the previous match operation.

If the match was successful but the group specified failed to match any part of the input sequence, then defaultValue is returned, whereas group(String, int) would return null. Otherwise, the return is equivalent to that of m.group(groupName, occurrence).

As a note,

m.group(groupName, occurrence, null)

behaves in exactly the same way as

m.group(groupName, occurrence)

An invocation of this convenience method of the form

 m.group(groupName, occurrence, defaultValue)

behaves in exactly the same way as

 m.group(groupName + "[" + occurrence + "]", defaultValue)

Specified by:
group in interface MatchResult
Parameters:
groupName - The group name for a capturing group in this matcher's pattern
occurrence - The occurrence of the specified group name
defaultValue - The string to return if group(String, int) would return null
Returns:
The (possibly empty) subsequence captured by the group during the previous match, or defaultValue if the group failed to match part of the input
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group
See Also:
group(String, int)

groupCount

public int groupCount()
Returns the number of capturing groups in this matcher's pattern.

Group zero denotes the entire pattern by convention. It is not included in this count.

Any non-negative integer smaller than or equal to the value returned by this method is guaranteed to be a valid group index for this matcher.

Specified by:
groupCount in interface MatchResult
Returns:
The number of capturing groups in this matcher's pattern

groupCount

public int groupCount(int group)
Specified by:
groupCount in interface MatchResult
Returns:
Since:
0.2

groupCount

public int groupCount(java.lang.String groupName)
Returns the number of capturing groups (with the given group name) in this matcher's pattern.

Group zero denotes the entire pattern by convention. It is not included in this count.

Any non-negative integer smaller than or equal to the value returned by this method is guaranteed to be a valid occurrence (for a group, groupName[occurrence]) for this matcher.

If groupName is the empty string, this method's return is equal to the return from groupCount().

Note: unlike other methods, this method doesn't throw an exception if the specified group doesn't exist. Instead, zero is returned, since the number of groups with the given (non-existent) group name is zero.

Specified by:
groupCount in interface MatchResult
Parameters:
groupName - The group name for a capturing group in this matcher's pattern
Returns:
The number of capturing groups (with the given group name) in this matcher's pattern

matches

public boolean matches()
Attempts to match the entire region against the pattern.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:
true if, and only if, the entire region sequence matches this matcher's pattern

matched

public boolean matched()
Returns whether the match was successful.

Note that some patterns, for example a*, match the empty string. This method will return true when the pattern successfully matches the empty string in the input.

Note: unlike the other methods, this method won't throw an exception if no match has been attempted - instead, false will be returned. This method provides a way to check that a match has been attempted, and that it succeeded.

Specified by:
matched in interface MatchResult
Returns:
true if the match was successful

matched

public boolean matched(int group)
Returns whether the specified group matched any part of the input sequence.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.matched(0) is equivalent to m.matched().

If the match was successful but the group specified failed to match any part of the input sequence, then false is returned. Note that some groups, for example (a*), match the empty string. This method will return true when such a group successfully matches the empty string in the input.

Specified by:
matched in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
true if in the previous match, the group matched part of the input.
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

matched

public boolean matched(java.lang.String group)
Returns whether the specified group matched any part of the input sequence.

If the match was successful but the group specified failed to match any part of the input sequence, then false is returned. Note that some groups, for example (a*), match the empty string. This method will return true when such a group successfully matches the empty string in the input.

Specified by:
matched in interface MatchResult
Parameters:
group - A capturing group in this matcher's pattern
Returns:
true if in the previous match, the group matched part of the input.
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

matched

public boolean matched(java.lang.String groupName,
                       int occurrence)
Returns whether the specified group matched any part of the input sequence.

If the match was successful but the group specified failed to match any part of the input sequence, then false is returned. Note that some groups, for example (a*), match the empty string. This method will return true when such a group successfully matches the empty string in the input.

An invocation of this convenience method of the form

m.matched(groupName, occurrence)

behaves in exactly the same way as

 m.matched(groupName + "[" + occurrence + "]")

Specified by:
matched in interface MatchResult
Parameters:
groupName - The group name for a capturing group in this matcher's pattern
occurrence - The occurrence of the specified group name
Returns:
true if in the previous match, the group matched part of the input.
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

isEmpty

public boolean isEmpty()
Returns whether the previous match matched the empty string.

Specified by:
isEmpty in interface MatchResult
Returns:
true if the previous match matched the empty string.
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed

isEmpty

public boolean isEmpty(int group)
Returns whether the specified group matched the empty string.

Capturing groups are indexed from left to right, starting at one. Group zero denotes the entire pattern, so the expression m.isEmpty(0) is equivalent to m.isEmpty().

If the match was successful but the group specified failed to match any part of the input sequence, then false is returned.

Specified by:
isEmpty in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
true if in the previous match, the group matched the empty string.
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IndexOutOfBoundsException - If there is no capturing group in the pattern with the given index

isEmpty

public boolean isEmpty(java.lang.String group)
Returns whether the specified group matched the empty string.

If the match was successful but the group specified failed to match any part of the input sequence, then false is returned.

Specified by:
isEmpty in interface MatchResult
Parameters:
group - A capturing group in this matcher's pattern
Returns:
true if in the previous match, the group matched the empty string.
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

isEmpty

public boolean isEmpty(java.lang.String groupName,
                       int occurrence)
Returns whether the specified group matched the empty string.

If the match was successful but the group specified failed to match any part of the input sequence, then false is returned.

An invocation of this convenience method of the form

m.isEmpty(groupName, occurrence)

behaves in exactly the same way as

 m.isEmpty(groupName + "[" + occurrence + "]")

Specified by:
isEmpty in interface MatchResult
Parameters:
groupName - The group name for a capturing group in this matcher's pattern
occurrence - The occurrence of the specified group name
Returns:
true if in the previous match, the group matched the empty string.
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If there is no capturing group in the pattern of the given group

find

public boolean find()
Attempts to find the next subsequence of the input sequence that matches the pattern.

This method starts at the beginning of this matcher's region, or, if a previous invocation of the method was successful and the matcher has not since been reset, at the first character not matched by the previous match.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:
true if, and only if, a subsequence of the input sequence matches this matcher's pattern

find

public boolean find(int start)
Resets this matcher and then attempts to find the next subsequence of the input sequence that matches the pattern, starting at the specified index.

If the match succeeds then more information can be obtained via the start, end, and group methods, and subsequent invocations of the find() method will start at the first character not matched by this match.

Parameters:
start - 0-based start index
Returns:
true if, and only if, a subsequence of the input sequence starting at the given index matches this matcher's pattern
Throws:
java.lang.IndexOutOfBoundsException - If start is less than zero or if start is greater than the length of the input sequence.

lookingAt

public boolean lookingAt()
Attempts to match the input sequence, starting at the beginning of the region, against the pattern.

Like the matches method, this method always starts at the beginning of the region; unlike that method, it does not require that the entire region be matched.

If the match succeeds then more information can be obtained via the start, end, and group methods.

Returns:
true if, and only if, a prefix of the input sequence matches this matcher's pattern

quoteReplacement

public static java.lang.String quoteReplacement(java.lang.String s)
Returns a literal replacement String for the specified String.

This method produces a String that will work as a literal replacement s in the appendReplacement method of the Matcher class. The String produced will match the sequence of characters in s treated as a literal sequence. Slashes ('\') and dollar signs ('$') will be given no special meaning.

Parameters:
s - The string to be literalized
Returns:
A literal string replacement

appendReplacement

public Matcher appendReplacement(java.lang.StringBuffer sb,
                                 java.lang.String replacement)
Implements a non-terminal append-and-replace step.

This method performs the following actions:

  1. It reads characters from the input sequence, starting at the append position, and appends them to the given string buffer. It stops after reading the last character preceding the previous match, that is, the character at index start() - 1.

  2. It appends the given replacement string to the string buffer.

  3. It sets the append position of this matcher to the index of the last character matched, plus one, that is, to end().

The replacement string may contain references to subsequences captured during the previous match: Each occurrence of $g will be replaced by the result of evaluating group(g). The first number after the $ is always treated as part of the group reference. Subsequent numbers are incorporated into g if they would form a legal group reference. Only the numerals '0' through '9' are considered as potential components of the group reference. If the second group matched the string "foo", for example, then passing the replacement string "$2bar" would cause "foobar" to be appended to the string buffer. A dollar sign ($) may be included as a literal in the replacement string by preceding it with a backslash (\$).

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

This method is intended to be used in a loop together with the appendTail and find methods. The following code, for example, writes one dog two dogs in the yard to the standard-outputSyntax stream:

 Pattern p = Pattern.compile("cat");
 Matcher m = p.matcher("one cat two cats in the yard");
 StringBuffer sb = new StringBuffer();
 while (m.find()) {
     m.appendReplacement(sb, "dog");
 }
 m.appendTail(sb);
 System.out.println(sb.toString());
 

Parameters:
sb - The target string buffer
replacement - The replacement string
Returns:
This matcher
Throws:
java.lang.IllegalStateException - If no match has yet been attempted, or if the previous match operation failed
java.lang.IllegalArgumentException - If the replacement string refers to a named-capturing group that does not exist in the pattern
java.lang.IllegalArgumentException - If the replacement string refers to a named-capturing group that does not exist in the pattern
java.lang.IndexOutOfBoundsException - If the replacement string refers to a capturing group that does not exist in the pattern

appendTail

public java.lang.StringBuffer appendTail(java.lang.StringBuffer sb)
Implements a terminal append-and-replace step.

This method reads characters from the input sequence, starting at the append position, and appends them to the given string buffer. It is intended to be invoked after one or more invocations of the appendReplacement method in order to copy the remainder of the input sequence.

Parameters:
sb - The target string buffer
Returns:
The target string buffer

replaceAll

public java.lang.String replaceAll(java.lang.String replacement)
Replaces every subsequence of the input sequence that matches the pattern with the given replacement string.

This method first resets this matcher. It then scans the input sequence looking for matches of the pattern. Characters that are not part of any match are appended directly to the result string; each match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

Given the regular expression a*b, the input "aabfooaabfooabfoob", and the replacement string "-", an invocation of this method on a matcher for that expression would yield the string "-foo-foo-foo-".

Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

Parameters:
replacement - The replacement string
Returns:
The string constructed by replacing each matching subsequence by the replacement string, substituting captured subsequences as needed

replaceFirst

public java.lang.String replaceFirst(java.lang.String replacement)
Replaces the first subsequence of the input sequence that matches the pattern with the given replacement string.

This method first resets this matcher. It then scans the input sequence looking for a match of the pattern. Characters that are not part of the match are appended directly to the result string; the match is replaced in the result by the replacement string. The replacement string may contain references to captured subsequences as in the appendReplacement method.

Note that backslashes (\) and dollar signs ($) in the replacement string may cause the results to be different than if it were being treated as a literal replacement string. Dollar signs may be treated as references to captured subsequences as described above, and backslashes are used to escape literal characters in the replacement string.

Given the regular expression dog, the input "zzzdogzzzdogzzz", and the replacement string "cat", an invocation of this method on a matcher for that expression would yield the string "zzzcatzzzdogzzz".

Invoking this method changes this matcher's state. If the matcher is to be used in further matching operations then it should first be reset.

Parameters:
replacement - The replacement string
Returns:
The string constructed by replacing the first matching subsequence by the replacement string, substituting captured subsequences as needed

region

public Matcher region(int start,
                      int end)
Sets the limits of this matcher's region. The region is the part of the input sequence that will be searched to find a match. Invoking this method resets the matcher, and then sets the region to start at the index specified by the start parameter and end at the index specified by the end parameter.

Depending on the transparency and anchoring being used (see useTransparentBounds and useAnchoringBounds), certain constructs such as anchors may behave differently at or around the boundaries of the region.

Parameters:
start - The index to start searching at (inclusive)
end - The index to end searching at (exclusive)
Returns:
this matcher
Throws:
java.lang.IndexOutOfBoundsException - If start or end is less than zero, if start is greater than the length of the input sequence, if end is greater than the length of the input sequence, or if start is greater than end.

regionStart

public int regionStart()
Reports the start index of this matcher's region. The searches this matcher conducts are limited to finding matches within regionStart (inclusive) and regionEnd (exclusive).

Returns:
The starting point of this matcher's region

regionEnd

public int regionEnd()
Reports the end index (exclusive) of this matcher's region. The searches this matcher conducts are limited to finding matches within regionStart (inclusive) and regionEnd (exclusive).

Returns:
the ending point of this matcher's region

hasTransparentBounds

public boolean hasTransparentBounds()
Queries the transparency of region bounds for this matcher.

This method returns true if this matcher uses transparent bounds, false if it uses opaque bounds.

See useTransparentBounds for a description of transparent and opaque bounds.

By default, a matcher uses opaque region boundaries.

Returns:
true iff this matcher is using transparent bounds, false otherwise.
See Also:
useTransparentBounds(boolean)

useTransparentBounds

public Matcher useTransparentBounds(boolean b)
Sets the transparency of region bounds for this matcher.

Invoking this method with an argument of true will set this matcher to use transparent bounds. If the boolean argument is false, then opaque bounds will be used.

Using transparent bounds, the boundaries of this matcher's region are transparent to lookahead, lookbehind, and boundary matching constructs. Those constructs can see beyond the boundaries of the region to see if a match is appropriate.

Using opaque bounds, the boundaries of this matcher's region are opaque to lookahead, lookbehind, and boundary matching constructs that may try to see beyond them. Those constructs cannot look past the boundaries so they will fail to match anything outside of the region.

By default, a matcher uses opaque bounds.

Parameters:
b - a boolean indicating whether to use opaque or transparent regions
Returns:
this matcher
See Also:
hasTransparentBounds()

hasAnchoringBounds

public boolean hasAnchoringBounds()
Queries the anchoring of region bounds for this matcher.

This method returns true if this matcher uses anchoring bounds, false otherwise.

See useAnchoringBounds for a description of anchoring bounds.

By default, a matcher uses anchoring region boundaries.

Returns:
true iff this matcher is using anchoring bounds, false otherwise.
See Also:
useAnchoringBounds(boolean)

useAnchoringBounds

public Matcher useAnchoringBounds(boolean b)
Sets the anchoring of region bounds for this matcher.

Invoking this method with an argument of true will set this matcher to use anchoring bounds. If the boolean argument is false, then non-anchoring bounds will be used.

Using anchoring bounds, the boundaries of this matcher's region match anchors such as ^ and $.

Without anchoring bounds, the boundaries of this matcher's region will not match anchors such as ^ and $.

By default, a matcher uses anchoring region boundaries.

Parameters:
b - a boolean indicating whether or not to use anchoring bounds.
Returns:
this matcher
See Also:
hasAnchoringBounds()

toString

public java.lang.String toString()

Returns the string representation of this matcher. The string representation of a Matcher contains information that may be useful for debugging. The exact format is unspecified.

Overrides:
toString in class java.lang.Object
Returns:
The string representation of this matcher

hitEnd

public boolean hitEnd()
Returns true if the end of input was hit by the search engine in the last match operation performed by this matcher.

When this method returns true, then it is possible that more input would have changed the result of the last search.

Returns:
true iff the end of input was hit in the last match; false otherwise

requireEnd

public boolean requireEnd()
Returns true if more input could change a positive match into a negative one.

If this method returns true, and a match was found, then more input could cause the match to be lost. 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:
true iff more input could change a positive match into a negative one.

text

public java.lang.String text()
Returns the string being matched.

Specified by:
text in interface MatchResult
Returns:
the string being matched

getGroupName

public java.lang.String getGroupName(int group)
Returns the group name (if any) for the specified group.

If a match has been successful, this function's return is the group name associated with the given group for this match. This group name is match independent, except, possibly, when the group is part of a "branch reset" pattern.

If there is no successful match, this function's return is the group name, only in the case that it is match independent. The only case where the group name is not match independent is when the group is part of a "branch reset" subpattern, and there are at least two groups with the given number.

For example, in the pattern (?|(?<group1a>1a)|(?<group1b>1b), the group name for group 1 depends on whether the pattern matches "1a" or "1b". In this case, an IllegalStateException is thrown, because a match is required to determine the group name.

If there is more than one occurrence of the group, the returned group name includes the occurrence - for example, myGroup[1]. If there is only one occurrence of the group, only the group name is returned - for example, myGroup.

Specified by:
getGroupName in interface MatchResult
Parameters:
group - The index of a capturing group in this matcher's pattern
Returns:
the group name for the specified group, or null if the group has no associated group name
Throws:
java.lang.IllegalStateException - If the group name is match dependent, and no match has yet been attempted, or if the previous match operation failed

treatNullAsEmptyString

public boolean treatNullAsEmptyString()
Specified by:
treatNullAsEmptyString in interface MatchResult
Returns:
Since:
0.2

treatNullAsEmptyString

public Matcher treatNullAsEmptyString(boolean treatNullAsEmptyString)
Parameters:
treatNullAsEmptyString -
Since:
0.2

containsKey

public boolean containsKey(java.lang.Object key)
Specified by:
containsKey in interface MatchResult
Returns:
Throws:
java.lang.IllegalArgumentException - If key is not a CharSequence or Number
ill
Since:
0.2

containsValue

public boolean containsValue(java.lang.Object value)
Specified by:
containsValue in interface MatchResult
Returns:
Since:
0.2

entrySet

public java.util.Set<java.util.Map.Entry<java.lang.Integer,java.lang.String>> entrySet()
Specified by:
entrySet in interface MatchResult
Returns:
Since:
0.2

get

public java.lang.String get(java.lang.Object key)
Specified by:
get in interface MatchResult
Returns:
Since:
0.2

keySet

public java.util.Set<java.lang.Integer> keySet()
Specified by:
keySet in interface MatchResult
Returns:
Since:
0.2

size

public int size()
Specified by:
size in interface MatchResult
Returns:
Since:
0.2

values

public java.util.List<java.lang.String> values()
Specified by:
values in interface MatchResult
Returns:
Since:
0.2

getEntry

public java.util.Map.Entry<java.lang.Integer,java.lang.String> getEntry(int group)
Since:
0.2

iterator

public java.util.Iterator<MatchResult> iterator()
Returns an Iterator which traverses each match.

Specified by:
iterator in interface java.lang.Iterable<MatchResult>
Parameters:
matcher - a Matcher object
Returns:
an Iterator for a Matcher
Since:
1.0
See Also:
Matcher.group()

getResults

public java.util.List<MatchResult> getResults()

asBoolean

public boolean asBoolean()
Alias for find().

Coerces a Matcher instance to a boolean value, for use in Groovy truth

Specified by:
asBoolean in interface MatchResult
Since:
0.2

getAt

public java.lang.Object getAt(int idx)
Alias for MatchResult.group(int)

In Groovy, adds support the subscript operator, e.g. matcher[group], for a regex Matcher.

Specified by:
getAt in interface MatchResult
Returns:

getAt

public java.util.List getAt(java.util.Collection<?> indexes)

getAt

public java.lang.String getAt(java.lang.String group)
Alias for MatchResult.group(String)

In Groovy, adds support the subscript operator, e.g. matcher[group], for a regex Matcher.

Specified by:
getAt in interface MatchResult
Returns:

next

public MatchResult next()