Groovy JDK

java.util.regex
Class Matcher

Method Summary
Object getAt(int idx)
Support the subscript operator, e

For an example using no group match,

def p = /ab[d|f]/
def m = "abcabdabeabf" =~ p
assert 2 == m
assert 2 == m
assert ! m
assert 0 == m
def matches = ["abd", "abf"]
for (i in 0
assert m[i] == matches[i]
}

For an example using group matches,

def p = /(?:ab([c|d|e|f]))/
def m = "abcabdabeabf" =~ p
assert 4 == m
assert m
assert 1 == m
def matches = [["abc", "c"], ["abd", "d"], ["abe", "e"], ["abf", "f"]]
for (i in 0
assert m[i] == matches[i]
}

For another example using group matches,

def m = "abcabdabeabfabxyzabx" =~ /(?:ab([d|x-z]+))/
assert 3 == m
assert m
assert 1 == m
def matches = [["abd", "d"], ["abxyz", "xyz"], ["abx", "x"]]
for (i in 0
assert m[i] == matches[i]
}
List getAt(Collection indices)
Select a List of values from a Matcher using a Collection to identify the indices to be selected
int getCount()
Find the number of Strings matched to the given Matcher
static Matcher getLastMatcher()
Get the last hidden matcher that the system used to do a match
boolean hasGroup()
Check whether a Matcher contains a group or not
Iterator iterator()
Retuns an {@link Iterator} which traverses each match
void setIndex(int idx)
Set the position of the given Matcher to the given index
long size()
Provide the standard Groovy size() method for Matcher
 
Method Detail

getAt

public Object getAt(int idx)
Support the subscript operator, e.g. matcher[index], for a regex Matcher.

For an example using no group match,

def p = /ab[d|f]/
def m = "abcabdabeabf" =~ p
assert 2 == m.count
assert 2 == m.size() // synonym for m.getCount()
assert ! m.hasGroup()
assert 0 == m.groupCount()
def matches = ["abd", "abf"]
for (i in 0..<m.count) {
assert m[i] == matches[i]
}

For an example using group matches,

def p = /(?:ab([c|d|e|f]))/
def m = "abcabdabeabf" =~ p
assert 4 == m.count
assert m.hasGroup()
assert 1 == m.groupCount()
def matches = [["abc", "c"], ["abd", "d"], ["abe", "e"], ["abf", "f"]]
for (i in 0..<m.count) {
assert m[i] == matches[i]
}

For another example using group matches,

def m = "abcabdabeabfabxyzabx" =~ /(?:ab([d|x-z]+))/
assert 3 == m.count
assert m.hasGroup()
assert 1 == m.groupCount()
def matches = [["abd", "d"], ["abxyz", "xyz"], ["abx", "x"]]
for (i in 0..<m.count) {
assert m[i] == matches[i]
}

Parameters:
idx - an index.
Returns:
object a matched String if no groups matched, list of matched groups otherwise.

getAt

public List getAt(Collection indices)
Select a List of values from a Matcher using a Collection to identify the indices to be selected.

Parameters:
indices - a Collection of indices.
Returns:
a String of the values at the given indices

getCount

public int getCount()
Find the number of Strings matched to the given Matcher.

Returns:
int the number of Strings matched to the given matcher.

getLastMatcher

public static Matcher getLastMatcher()
Get the last hidden matcher that the system used to do a match.

Returns:
the last regex matcher

hasGroup

public boolean hasGroup()
Check whether a Matcher contains a group or not.

Returns:
boolean true if matcher contains at least one group.

iterator

public Iterator iterator()
Retuns an {@link Iterator} which traverses each match.

Returns:
an Iterator for a Matcher
See:
Matcher#group().

setIndex

public void setIndex(int idx)
Set the position of the given Matcher to the given index.

Parameters:
idx - the index number.

size

public long size()
Provide the standard Groovy size() method for Matcher.

Returns:
the matcher's size (count)

Groovy JDK