Method RegEx():match() Foundation

Finds the first match.

Syntax
:match( <cSubject>, [<nStartPos>] ) --> aMatch
Parameters
<cSubject>
The subject string to search.
<nStartPos>
An optional starting position for the match. This defaults to 1.
Return

An array with information about the match, or an empty array ({}) if no match was found.

If a match is found, each element in the array represents a capture group. The first element always contains the so-called full match. It may be followed by one or more additional array elements, each representing a numbered capture group defined in the pattern. The information about each capture group is returned in a sub-array of the following format:

[1] - The matched text

[2] - The start position

[3] - The end position (pointing after the last character).

Description

Searches for the first occurrence of the pattern in the subject string and returns detailed match information. The pattern is automatically compiled on first use if not already compiled.

:match() returns the matched text and the start and end position for each capture group defined in the pattern. The helper methods :getMatchText(), :getMatchPos() and :getMatchRange() simplify retrieval of this information for a given capture group.

Get match text

oRegEx := RegEx():create("(\d+)-(\d+)") 
aMatch := oRegEx:match("Range: 10-20") 
oRegEx:destroy() 

? RegEx():getMatchText(aMatch)      // "10-20" (full match) 
? RegEx():getMatchText(aMatch, 1)   // "10" (first group) 
? RegEx():getMatchText(aMatch, 2)   // "20" (second group) 

Feedback

If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please use this form to report a documentation issue.