|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
public interface MatchResult
The result of a match operation.
This interface is an extension of Java's MatchResult
class.
Javadocs were copied and appended with the added functionality.
This interface contains query methods used to determine the
results of a match against a regular expression. The match boundaries,
groups and group boundaries can be seen but not modified through
a MatchResult
.
Starting with Version 0.2, MatchResult
extends
Iterable<MatchResult>
and is Groovified,
adding some useful functionality.
Given the following MatchResult:
Pattern pattern = Pattern.compile("(a)(b)(c)(d)");
Matcher matcher = pattern.matcher("abcd");
matcher.find();
MatchResult result = matcher.toMatchResult();
Loop through each group (by group number):
// Loops for group = 1 to 3 (group count)
for (int group : result.keySet()) {
/*
* Output:
*
* result[1] = a
* result[2] = b
* result[3] = c
*/
System.out.printf("result[%d] = %s%n", group, result.group(group));
}
Loop through each group's value:
// Loops for each value
for (String groupValue : result.values()) {
/*
* Output:
*
* Value = a
* Value = b
* Value = c
*/
System.out.printf("Value = %s%n", groupValue);
}
Matcher
Method Summary | |
---|---|
boolean |
asBoolean()
Alias for matched() . |
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 this match. |
int |
end(java.lang.String group)
Returns the offset after the last character of the subsequence captured by the given group during this match. |
int |
end(java.lang.String groupName,
int occurrence)
Returns the offset after the last character of the subsequence captured by the given group during this match. |
java.util.Set<java.util.Map.Entry<java.lang.Integer,java.lang.String>> |
entrySet()
|
java.lang.String |
get(java.lang.Object key)
|
java.lang.Object |
getAt(int group)
Alias for group(int) |
java.lang.String |
getAt(java.lang.String group)
Alias for group(String) |
java.lang.String |
getGroupName(int group)
Returns the group name (if any) for the specified group. |
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 match result'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 match result's pattern. |
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. |
java.util.Set<java.lang.Integer> |
keySet()
|
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. |
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. |
int |
size()
|
int |
start()
Returns the start index of the match. |
int |
start(int group)
Returns the start index of the subsequence captured by the given group during this match. |
int |
start(java.lang.String group)
Returns the start index of the subsequence captured by the given group during this match. |
int |
start(java.lang.String groupName,
int occurrence)
Returns the start index of the subsequence captured by the given group during this match. |
java.lang.String |
text()
Returns the string being matched. |
boolean |
treatNullAsEmptyString()
|
java.util.List<java.lang.String> |
values()
|
Methods inherited from interface java.lang.Iterable |
---|
iterator |
Method Detail |
---|
int start()
java.lang.IllegalStateException
- If no match has yet been attempted, or if the previous match
operation failedint start(int group)
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().
group
- The index of a capturing group in this matcher's pattern
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 indexint start(java.lang.String group)
group
- A capturing group in this matcher's pattern
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 groupint start(java.lang.String groupName, int occurrence)
An invocation of this convenience method of the form
m.start(groupName, occurrence)
behaves in exactly the same way as
m.start(groupName + "[" + occurrence + "]")
groupName
- The group name for a capturing group in this matcher's patternoccurrence
- The occurrence of the specified group name
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 groupint end()
java.lang.IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failedint end(int group)
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().
group
- The index of a capturing group in this matcher's pattern
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 indexint end(java.lang.String group)
group
- A capturing group in this matcher's pattern
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 groupint end(java.lang.String groupName, int occurrence)
An invocation of this convenience method of the form
m.end(groupName, occurrence)
behaves in exactly the same way as
m.end(groupName + "[" + occurrence + "]")
groupName
- The group name for a capturing group in this matcher's patternoccurrence
- The occurrence of the specified group name
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 groupint occurrence(int groupIndex)
Branch reset patterns allow multiple capture groups with the same group index to exist. This method offers a way to determine which occurrence matched.
groupIndex
- the name of the group
int occurrence(java.lang.String groupName)
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.
groupName
- the name of the group
java.lang.String group()
For a matcher m with input sequence s, the expressions om.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.
java.lang.IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failedjava.lang.String group(int group)
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.
group
- The index of a capturing group in this matcher's pattern
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 indexjava.lang.String group(int group, java.lang.String defaultValue)
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)
group
- The index of a capturing group in this matcher's patterndefaultValue
- The string to return if group(int)
would return
null
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 indexgroup(int)
java.lang.String group(java.lang.String 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.
group
- A capturing group in this matcher's pattern
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 groupjava.lang.String group(java.lang.String group, java.lang.String defaultValue)
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)
group
- A capturing group in this matcher's patterndefaultValue
- The string to return if group(String)
would return
null
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 groupgroup(String)
java.lang.String group(java.lang.String groupName, int occurrence)
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 + "]")
groupName
- The group name for a capturing group in this matcher's patternoccurrence
- The occurrence of the specified group name
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 groupjava.lang.String group(java.lang.String groupName, int occurrence, java.lang.String defaultValue)
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)
groupName
- The group name for a capturing group in this matcher's patternoccurrence
- The occurrence of the specified group namedefaultValue
- The string to return if group(String, int)
would
return null
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 groupgroup(String, int)
int groupCount()
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.
int groupCount(int group)
group
-
int groupCount(java.lang.String groupName)
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.
groupName
- The group name for a capturing group in this matcher's pattern
boolean matched()
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.
true
if the match was successfulboolean matched(int group)
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.
group
- The index of a capturing group in this matcher's pattern
true
if in the previous match, the group matched
part of the input.
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 indexboolean matched(java.lang.String group)
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.
group
- A capturing group in this matcher's pattern
true
if in the previous match, the group matched
part of the input.
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 groupboolean matched(java.lang.String groupName, int occurrence)
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 + "]")
groupName
- The group name for a capturing group in this matcher's patternoccurrence
- The occurrence of the specified group name
true
if in the previous match, the group matched
part of the input.
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 groupboolean isEmpty()
true
if the previous match matched the empty string.
java.lang.IllegalStateException
- If no match has yet been attempted,
or if the previous match operation failedboolean isEmpty(int group)
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.
group
- The index of a capturing group in this matcher's pattern
true
if in the previous match, the group matched
the empty string.
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 indexboolean isEmpty(java.lang.String group)
If the match was successful but the group specified failed to match any part of the input sequence, then false is returned.
group
- A capturing group in this matcher's pattern
true
if in the previous match, the group matched
the empty string.
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 groupboolean isEmpty(java.lang.String groupName, int occurrence)
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 + "]")
groupName
- The group name for a capturing group in this matcher's patternoccurrence
- The occurrence of the specified group name
true
if in the previous match, the group matched
the empty string.
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 groupPattern pattern()
java.lang.String text()
java.lang.String getGroupName(int 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
.
group
- The index of a capturing group in this matcher's pattern
null
if
the group has no associated group name
java.lang.IllegalStateException
- If the group name is match dependent, and no match has yet
been attempted, or if the previous match operation failedboolean treatNullAsEmptyString()
boolean containsKey(java.lang.Object key)
key
-
boolean containsValue(java.lang.Object value)
value
-
java.util.Set<java.util.Map.Entry<java.lang.Integer,java.lang.String>> entrySet()
java.lang.String get(java.lang.Object key)
key
-
java.util.Set<java.lang.Integer> keySet()
int size()
java.util.List<java.lang.String> values()
boolean asBoolean()
matched()
.
Coerces a Matcher instance to a boolean value, for use in Groovy truth
java.lang.Object getAt(int group)
group(int)
In Groovy, adds support the subscript operator, e.g. matcher[group], for a regex Matcher.
group
-
java.lang.String getAt(java.lang.String group)
group(String)
In Groovy, adds support the subscript operator, e.g. matcher[group], for a regex Matcher.
group
-
|
|||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |