RegexCapture (Stringlist function): Difference between revisions
m (1 revision) |
m (1 revision) |
(No difference)
|
Revision as of 22:34, 10 June 2012
Capture substrings to Stringlist using regex (Stringlist class)
This method applies a regular expression, or "regex," to a given input string, obtains those characters in the string that match the "capturing groups" in the regex, and appends these captured strings to the method Stringlist.
Within a regex, characters enclosed by a pair of unescaped parentheses form a "subexpression". A subexpression is a capturing group if the opening parenthesis is not followed by a question mark (?
). Each set of characters matched (captured) by a RegexCapture capturing group is appended to the method object Stringlist as a separate item. RegexCapture uses the rules of regular expression matching (information about which is provided in "Regex processing rules"). RegexCapture accepts two required and two optional arguments and, optionally, returns a numeric value. RegexCapture is a callable method.
Syntax
[%rc =] sl:RegexCapture( string, regex, [Options= string], [Status= %output]) Throws InvalidRegex
Syntax terms
%rc | If specified, a numeric variable that is set to 0 if the regular expression was invalid or no match was found, or it is the position of the character after the last character matched. | ||||||||
---|---|---|---|---|---|---|---|---|---|
sl | A Stringlist object. | ||||||||
string | The input string, to which the regular expression regex is applied. | ||||||||
regex | A string that is interpreted as a regular expression and is applied to the input string argument to determine the substrings captured from string. | ||||||||
Options | This is an optional, name required, parameter supplying a string of single letter options, which may be specified in uppercase or lowercase, in any combination, and blank separated or not as you prefer. For more information about these options, see "Common regex options".
| ||||||||
Status | The Status argument (name required) is optional; if specified, it is set to an integer code. These values are possible:
|
Usage notes
- All errors in RegexCapture, including invalid argument(s) result in request cancellation.
- It is strongly recommended that you protect your environment from regular expression processing demands on PDL and STBL space by setting, say,
UTABLE LPDLST 3000
andUTABLE LSTBL 9000
. See "User Language programming considerations". - If %rc is 0, either regex did not match string, or there was an error in the regex. See the Status argument for additional information: If it is negative, it indicates an error. If it is zero, it indicates there was no error, but the regex did not match.
- Even with a Status value of
1
, which indicates a successful match, it is possible that zero items were added to the method argument Stringlist. This is the case if the regex contains no capturing groups. Otherwise, each capturing group in the regex creates an item in the Stringlist, even if that item contains only the null string. - It is indistinguishable whether an empty item in the output Stringlist represents a capturing group in the regex that was applied but matched no characters, or represents a capturing group that was not applied for some reason (for example, an earlier alternative made the match).
- For information about additional methods and $functions that support regular expressions, see "Regex processing".
- RegexCapture is available as of Sirius Mods Version 6.9.
Examples
-
In the following code fragment, the regex, which has three groups, matches the string. Two items are added to the method Stringlist, only one of which is non-null:
... %sl = new %regex = 'a(b)(?:c)(d?)' %inStr = 'abc' %pos = %sl:RegexCapture (%inStr, %regex, Status=%st) If not %pos then Print 'Status from RegexCapture is ' %st Else Print %regex ' matches ' %inStr End If For %i from 1 to %sl:Count Print 'Captured item ' %i ' is: ' %sl:Item(%i) End For
This code would print the following:
a(b)(?:c)(d?) matches abc Captured item 1 is: b Captured item 2 is:
- In this example:
- Of the three groups, those expressions within unescaped parentheses, two are capturing groups:
(b)
and(d?)
. The(?:c)
group starts with?:
and therefore is a non-capturing group. - The
a
in the regex matches thea
in the input string, but it is not in a capturing group, soa
is not placed on the output Stringlist. - The
b
in the regex matches theb
in the input string and is in a capturing group, sob
is placed on the Stringlist. - The
c
in the regex matches thec
in the input string, but it is in a non-capturing group, soc
is not placed on the Stringlist. - Because there are no d's to match, the
d?
in the regex (the question mark after thed
indicates zero or one match) matches a null string, so a null string is placed on the Stringlist.
The order of the capturing groups is determined by the order of the open parenthesis corresponding to the capturing group in the regular expression. So, if the above example is changed to the following:
... %sl = new %regex = '(a(b)(?:c)(d?))' %inStr = 'abc' %pos = %sl:RegexCapture (%inStr, %regex, Status=%st) ... For %i from 1 to %sl:Count Print 'Captured item ' %i ' is: ' %sl:Item(%i) End For
The following would be printed:
Captured item 1 is: abc Captured item 2 is: b Captured item 3 is:
This results because a new capturing group that contained the entire regex from the previous example was added. Since there are no extra match conditions in this group, the string still matches in exactly the same way, but all the matching parts are now part of this group. Since this outermost group's left-most parenthesis comes before the others, its matching string is first on the Stringlist.
- Of the three groups, those expressions within unescaped parentheses, two are capturing groups:
- If a capturing group matches more than one set of characters, all the matched characters are output onto the Stringlist item corresponding with that group. For example, if this is the regex and input string:
... %sl = new %regex = '(.(.))+' %inStr = 'abcdefghijklmnopqrstuvwxyz' %pos = %sl:RegexCapture (%inStr, %regex) ... For %i from 1 to %sl:Count Print 'Captured item ' %i ' is: ' %sl:Item(%i) End For
Then the following would be printed:
Captured item 1 is: abcdefghijklmnopqrstuvwxyz Captured item 2 is: bdfhjlnprtvxz
This results because the regular expression
(.(.))+
matches any number of pairs of characters (the dot "." matches any character), each pair being associated with the capturing group(.(.))
formed by the outer parentheses. Every other character is also in the capturing group(.)
formed by the inner parentheses. The matches are concatenated on to the Stringlist item associated with the capturing group, so all pairs of characters (and so all characters) are concatenated on to the first Stringlist item, and every other character is concatenated on to the second Stringlist item.This concatenation is somewhat different from Perl -- Perl outputs only the last match for each capturing group, In this example, Perl would set
$1
(corresponding to Stringlist item 1) toyz
and$2
toz
. - On a match, RegexCapture returns the position after the matching string, making it easy to split the string at the point of the match. For example, the following statements:
... %sl = new %regex = '([+\-*/])' %inStr = '133*765' %pos = %sl:RegexCapture (%inStr, %regex) print 'Captured item is ' %sl(1) print 'After it comes ' $substr(%instr, %pos)
Would print this result:
Captured item is * After it comes 765
The capturing group looks for a single arithmetic operator character, and it places it on the output Stringlist.
%pos
, the position after the matching character, is returned and used to retrieve the string after the matching character. Note that the hyphen is a metacharacter in a character class, so it must be escaped in the regex here. For the plus sign and asterisk characters, which are metacharacters outside a character class but not when inside one, escaping is optional. If the input string contained multiple numbers separated by arithmetic operators, you could use the RegexSplit to apply the regex repeatedly to the string and collect in a Stringlist the numbers that were separated by the operators.See also