Match CLASS

A stateful object tracking the result from the previous matching attempt.

The instances of this class are created by APIs on Regex. In addition to reporting whether it's a match, this class also provides break-down information such as the indexes of the matched region within the given input and the text for each captured group.

Once created, a Match object is associated with a Regex pattern. Methods such as next() will be called against that particular regex.

Parent Class

See Also

All Members


TypeNameSignature
methodgetEndpublic int getEnd()
methodgetGrouppublic String getGroup(int)
methodgetGroupspublic int getGroups()
methodgetStartpublic int getStart()
methodgetTextpublic String getText()
methodisMatchedpublic bool isMatched()
methodnextpublic bool next()

Methods


public int getEnd()

Get the 0-based end index of matched region.

Returns

  • The end index of matched region, if matched; undefined if not.

public String getGroup(int index)

Get the text for each captured group.

The groups are captured in the order of left parenthesis. The group 0 is reserved for the entire matched region and thus is always available should the match succeed. Therefore suppose getGroups() returns N, this method can be called with any integer between 0 and N, inclusive. An example:

   Regex r = new Regex("(a?(b*)c?)(e+)");
   Match m = r.matchAll("abee");
   m.getGroups(); // 3
   m.getGroup(0); // The entire matched region - equivalent to getText()
   m.getGroup(1); // ab
   m.getGroup(2); // b
   m.getGroup(3); // ee

Parameters

  • index The group index.

Returns

  • The captured group text.

Throws


public int getGroups()

Get the count of groups. Each () pair in a pattern forms one group. Note since a group may fall under an optional part of the regex, the value returned by this method is not necessarily equal to the total count of pairs in the pattern.

This method doesn't count group 0, defined as entire matched region.

Returns

  • The count of groups. 0 if no group is found in the pattern.

public int getStart()

Get the 0-based start index of matched region.

Returns

  • The start index of matched region, if matched; undefined if not.

public String getText()

Get the text of the matched region.

Returns

  • The text of the matched region. Note in the case of successful full-match this is same to the match input.

public bool isMatched()

Whether the previous attempt at matching, possibly driven by either next() or matchAll(string), was successful.

Returns

  • True if the previous matching was successful.

public bool next()

Try to find the next occurrence of the pattern.

If this Match object was returned from Regex.matchAll(string), calling this method will return false.

Returns

  • True if found, with tracking info updated. The user may call methods such as getStart to get the matching details. False if not found, and at this point results from those methods are undefined.