RegexCapture (Stringlist function)

From m204wiki
Revision as of 23:30, 27 January 2011 by Dme (talk | contribs) (→‎Examples)
Jump to navigation Jump to search

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). 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 The Options argument (name required) is an optional string of options. The options are single letters, which may be specified in uppercase or lowercase, in any combination, and separated by blanks or not separated. For more information about these options, see Regex processing.
I Do case-insensitive matching between string and regex.
S Dot-All mode: a dot (.) can match any character, including carriage return and linefeed.
M Multi-line mode: let anchor characters match end-of-line indicators wherever the indicator appears in the input string. M mode is ignored if C (XML Schema) mode is specified.
C Do the match according to XML Schema regex rules. Each regex is implicitly anchored at the beginning and end, and no characters serve as anchors. For more information, see Regex processing.
Status The Status argument (name required) is optional; if specified, it is set to an integer code. These values are possible:
>0 A successful match was obtained. This integer is the position of the character after the last character matched.
0 No match: string is not matched by regex.
-1nnn The pattern in regex is invalid. nnn, the absolute value of the return minus 1000, gives the 1-based position of the character being scanned when the error was discovered. The value for an error occurring at end-of-string is the length of the string + 1. Prior to Version 7.0 of the Sirius Mods, an invalid regex results in a Status value of -1.
Note: If you omit this argument and a negative Status value is to be returned, the run is cancelled.

Usage notes

  • All errors in RegexCapture, including invalid argument(s) result in request cancellation.
  • It is strongly recommended that you protect your environment from regex processing demands on PDL and STBL space by setting, say, UTABLE LPDLST 3000 and UTABLE LSTBL 9000. For further discussion of this, see User Language coding 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.
  • Available as of Sirius Mods Version 6.9

Examples

  1. 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:

  2. 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 the a in the input string, but it is not in a capturing group, so a is not placed on the output Stringlist.
    • The b in the regex matches the b in the input string and is in a capturing group, so b is placed on the Stringlist.
    • The c in the regex matches the c in the input string, but it is in a non-capturing group, so c is not placed on the Stringlist.
    • Because there are no d's to match, the d? in the regex (the question mark after the d 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.

  3. 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) to yz and $2 to z.

  4. 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