Match (Regex function)
Position after match of regex (Regex class)
This function tests a Unicode or EBCDIC string against the regular expression in the Regex object. It can also return the contents of capturing groups to a Stringlist.
Syntax
%number = regex:Match( string, [Start= number], [Capture= stringlist])
Syntax terms
%number | The position in the string after the match. If the string does not match the regular expression, 0 is returned. If the regular expressions matches up to the end of the string, the lenght of the string plus one is returned. |
---|---|
regex | The Regex object |
string | The Unicode or EBCDIC string to test against the Regex object. |
Start | number The starting position. This value must be between 1 and the length of string plus 1. The default value is 1, which means to start at the beginning of the string. |
Capture | A Stringlist object that is to receive the capturing group contents if there is a match. Each added item represents a capturing group's value with items added in capturing group order. |
Usage notes
- There is no Capture method for the Regex class since Match provides capture functionality.
- An advantage of this method over the String RegexMatch method is that this method has a start position parameter.
Examples
The following illustrates how a loop can find all occurrences of a particular pattern in a string and to add those occurrences to a Stringlist:
%pos is float %matches is object stringlist %regex is object regex %str is longstring ... %regex = new("(<[^>]+>)") %pos = 1 %matches = new repeat while %pos %pos = %regex:match(%str, start=%pos, capture=%matches) end repeat
Note that no special check is required for a zero-length string – 1 is a valid start position for a zero-length string and the first Match call for a zero length string will always return 0 in this case.