RegexCapture (Stringlist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
mNo edit summary
No edit summary
 
(13 intermediate revisions by 7 users not shown)
Line 1: Line 1:
{{Template:Stringlist:RegexCapture subtitle}}
{{Template:Stringlist:RegexCapture subtitle}}


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 <var>Stringlist</var>.
This [[Notation conventions for methods#Callable functions|callable]] 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 <var>Stringlist</var>.


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 <b><i>not</i></b> followed by a question mark (<code>?</code>). Each set of characters matched (captured) by a <var>RegexCapture</var> capturing group is appended to the method object <var>Stringlist</var> as a separate item. <var>RegexCapture</var> uses the rules of regular expression matching (information about which is provided in <var>[[Regex_processing#Regex_rules|"Regex processing rules"]]</var>). <var>RegexCapture</var> accepts two required and two optional arguments and, optionally, returns a numeric value.  <var>RegexCapture</var> is a callable method.
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 <b><i>not</i></b> followed by a question mark (<tt>?</tt>). Each set of characters matched (captured) by a <var>RegexCapture</var> capturing group is appended to the method object <var>Stringlist</var> as a separate item. <var>RegexCapture</var> uses the rules of regular expression matching (information about which is provided in [[Regex_processing#Regex_rules|"Regex processing rules"]]).  


==Syntax==
==Syntax==
{{Template:Stringlist:RegexCapture syntax}}
{{Template:Stringlist:RegexCapture syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%rc</th>
<tr><th>%rc</th>
<td>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. </td></tr>
<td>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. </td></tr>
<tr><th>sl</th>
<tr><th>sl</th>
<td>A <var>Stringlist</var> object. </td></tr>
<td>A <var>Stringlist</var> object. </td></tr>
<tr><th>string</th>
<tr><th>string</th>
<td>The input string, to which the regular expression <var class="term">regex</var> is applied. </td></tr>
<td>The input string, to which the regular expression <var class="term">regex</var> is applied. </td></tr>
<tr><th>regex</th>
<tr><th>regex</th>
<td>A string that is interpreted as a regular expression and is applied to the input <var class="term">string</var> argument to determine the substrings captured from <var class="term">string</var>. </td></tr>
<td>A string that is interpreted as a regular expression and is applied to the input <var class="term">string</var> argument to determine the substrings captured from <var class="term">string</var>. </td></tr>
<tr><th><var>Options</var></th>
<tr><th><var>Options</var></th>
<td>This is an optional, but [[Notation conventions for methods#Named parameters|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 <var>[[Regex_processing#Common_regex_options|"Common regex options"]]</var>.
<td>This is an optional, [[Notation conventions for methods#Named parameters|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 [[Regex_processing#Common_regex_options|Common regex options]].
<table class="syntaxNested">
</td></tr>
<tr><th><var>I</var></th>
 
<td>Do case-insensitive matching between <var class="term">string</var> and <var class="term">regex</var>.</td></tr>
<tr><th><var>S</var></th>
<td>Dot-All mode: a period (<code>.</code>) can match any character, including carriage return and linefeed.</td></tr>
<tr><th><var>M</var></th>
<td>Multi-line mode: let anchor characters match end-of-line indicators <b><i>wherever</i></b> the indicator appears in the input string.  <var>M</var> mode is ignored if <var>C</var> (XML Schema) mode is specified.</td></tr>
<tr><th><var>C</var></th>
<td>Do the match according to <var>[[Regex_processing#XML_Schema_mode|"XML Schema regex rules"]]</var>.  Each <var class="term">regex</var> is implicitly anchored at the beginning and end, and no characters serve as anchors.</td></tr>
</table></td></tr>
<tr><th><var>Status</var></th>
<tr><th><var>Status</var></th>
<td>The <var>Status</var> argument (name required) is optional; if specified, it is set to an integer code. These values are possible:
<td>The <var>Status</var> argument (name required) is optional; if specified, it is set to an integer code. These values are possible:
<table class="syntaxNested">
<table class="syntaxNested">
<tr><th><var>>0</var></th>
<tr><th><var>>0</var></th>
<td>A successful match was obtained. This integer is the position of the character <b><i>after</i></b> the last character matched. </td></tr>
<td>A successful match was obtained. This integer is the position of the character <b><i>after</i></b> the last character matched. </td></tr>
<tr><th><var> 0</var></th>
<tr><th><var> 0</var></th>
<td>No match: <var class="term">string</var> is not matched by <var class="term">regex</var>. </td></tr>
<td>No match: <var class="term">string</var> is not matched by <var class="term">regex</var>. </td></tr>
<tr><th><var>-1</var>nnn</th>
<tr><th><var>-1</var>nnn</th>
<td>The pattern in <var class="term">regex</var> is invalid. <i>nnn</i> (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 <var class="product">Sirius Mods</var>, an invalid <var class="term">regex</var> results in a Status value of <code>-1</code>. </td></tr>
<td>The pattern in <var class="term">regex</var> is invalid. <i>nnn</i> (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 <var class="product">Sirius Mods</var>, an invalid <var class="term">regex</var> results in a Status value of <code>-1</code>. </td></tr>
</table>
</table>
<b>Note:</b> If you omit this argument and a negative status value is to be returned, the run is cancelled.</td></tr>
<b>Note:</b> If you omit this argument and a negative status value is to be returned, the request is cancelled.</td></tr>
</table>
</table>


==Usage notes==
==Usage notes==
<ul><li>All errors in <var>RegexCapture</var>, including invalid argument(s) result in request cancellation.
<ul>
<li>It is strongly recommended that you protect your environment from regular expression processing demands on PDL and STBL space by setting, say, <code>UTABLE LPDLST 3000</code> and <code>UTABLE LSTBL 9000</code>. See <var>[[Regex_processing#User_Language_programming_considerations|"User Language programming considerations"]]</var>.
<li>All errors in <var>RegexCapture</var>, including invalid argument(s) result in request cancellation.
 
<li>It is strongly recommended that you protect your environment from regular expression processing demands on PDL and STBL space by setting, say, <code>UTABLE LPDLST 3000</code> and <code>UTABLE LSTBL 9000</code>. See [[Regex processing#SOUL programming considerations|SOUL programming considerations]].
 
<li>If <var class="term">%rc</var> is 0, either <var class="term">regex</var> did not match <var class="term">string</var>, or there was an error in the regex. See the <var>Status</var> 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.
<li>If <var class="term">%rc</var> is 0, either <var class="term">regex</var> did not match <var class="term">string</var>, or there was an error in the regex. See the <var>Status</var> 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.
<li>Even with a <var>Status</var> value of <code>1</code>, which indicates a successful match, it is possible that zero items were added to the method argument <var>Stringlist</var>. This is the case if the regex contains no capturing groups. Otherwise, each capturing group in the regex creates an item in the <var>Stringlist</var>, even if that item contains only the null string.
<li>Even with a <var>Status</var> value of <code>1</code>, which indicates a successful match, it is possible that zero items were added to the method argument <var>Stringlist</var>. This is the case if the regex contains no capturing groups. Otherwise, each capturing group in the regex creates an item in the <var>Stringlist</var>, even if that item contains only the null string.
<li>It is indistinguishable whether an empty item in the output <var>Stringlist</var> 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).
<li>It is indistinguishable whether an empty item in the output <var>Stringlist</var> 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).
<li>For information about additional methods and $functions that support regular expressions, see <var>[[Regex processing|"Regex processing"]]</var>.
 
<li><var>RegexCapture</var> is available as of <var class="product">[[Sirius Mods|"Sirius Mods"]]</var> Version 6.9.</ul>
<li>For information about additional methods and $functions that support regular expressions, see [[Regex processing]].
</ul>


==Examples==
==Examples==
Line 55: Line 61:
In the following code fragment, the <var class="term">regex</var>, which has three groups, matches the <var class="term">string</var>. Two items are added to the method <var>Stringlist</var>, only one of which is non-null:
In the following code fragment, the <var class="term">regex</var>, which has three groups, matches the <var class="term">string</var>. Two items are added to the method <var>Stringlist</var>, only one of which is non-null:


<p class="code"> ...
<p class="code">...
%sl = new
%sl = new
%regex = 'a(b)(?:c)(d?)'
%regex = 'a(b)(?:c)(d?)'
Line 86: Line 92:
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:
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:


<p class="code"> ...
<p class="code">...
%sl = new
%sl = new
%regex = '(a(b)(?:c)(d?))'
%regex = '(a(b)(?:c)(d?))'
Line 103: Line 109:
This results because a new capturing group that contained the entire <var class="term">regex</var> from the previous example was added. Since there are no extra match conditions in this group, the <var class="term">string</var> 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 <var>Stringlist</var>.
This results because a new capturing group that contained the entire <var class="term">regex</var> from the previous example was added. Since there are no extra match conditions in this group, the <var class="term">string</var> 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 <var>Stringlist</var>.
<li>If a capturing group matches more than one set of characters, all the matched characters are output onto the <var>Stringlist</var> item corresponding with that group. For example, if this is the <var class="term">regex</var> and input string:
<li>If a capturing group matches more than one set of characters, all the matched characters are output onto the <var>Stringlist</var> item corresponding with that group. For example, if this is the <var class="term">regex</var> and input string:
<p class="code"> ...
<p class="code">...
%sl = new
%sl = new
%regex = '(.(.))+'
%regex = '(.(.))+'
Line 117: Line 123:
Captured item 2 is: bdfhjlnprtvxz
Captured item 2 is: bdfhjlnprtvxz
</p>
</p>
This results because the regular expression <code>(.(.))+</code> matches any number of pairs of characters (the dot "." matches any character), each pair being associated with the capturing group <code>(.(.))</code> formed by the outer parentheses. Every other character is also in the capturing group <code>(.)</code> formed by the inner parentheses. The matches are concatenated on to the <var>Stringlist</var> item associated with the capturing group, so all pairs of characters (and so all characters) are concatenated on to the first <var>Stringlist</var> item, and every other character is concatenated on to the second <var>Stringlist</var> item.
This results because the regular expression <code>(.(.))+</code> matches any number of pairs of characters (the dot (<tt>.</tt>) matches any character), each pair being associated with the capturing group <code>(.(.))</code> formed by the outer parentheses. Every other character is also in the capturing group <code>(.)</code> formed by the inner parentheses. The matches are concatenated on to the <var>Stringlist</var> item associated with the capturing group, so all pairs of characters (and so all characters) are concatenated on to the first <var>Stringlist</var> item, and every other character is concatenated on to the second <var>Stringlist</var> item.


This concatenation is somewhat different from Perl -- Perl outputs only the <b><i>last</i></b> match for each capturing group, In this example, Perl would set <code>$1</code> (corresponding to <var>Stringlist</var> item 1) to <code>yz</code> and <code>$2</code> to <code>z</code>.
This concatenation is somewhat different from Perl &mdash; Perl outputs only the <b><i>last</i></b> match for each capturing group, In this example, Perl would set <code>$1</code> (corresponding to <var>Stringlist</var> item 1) to <code>yz</code> and <code>$2</code> to <code>z</code>.


<LI>On a match, <var>RegexCapture</var> returns the position after the matching string, making it easy to split the <var class="term">string</var> at the point of the match. For example, the following statements:
<li>On a match, <var>RegexCapture</var> returns the position after the matching string, making it easy to split the <var class="term">string</var> at the point of the match. For example, the following statements:
<p class="code">...
<p class="code">...
%sl = new
%sl = new
Line 135: Line 141:
</p>
</p>


The capturing group looks for a single arithmetic operator character, and it places it on the output <var>Stringlist</var>. <code>%pos</code>, 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 <var class="term">regex</var> 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 <var class="term">string</var> contained multiple numbers separated by arithmetic operators, you could use the <var>[[RegexSplit (Stringlist function)|RegexSplit]]</var> to apply the <var class="term">regex</var> repeatedly to the <var class="term">string</var> and collect in a <var>Stringlist</var> the numbers that were separated by the operators.
The capturing group looks for a single arithmetic operator character, and it places it on the output <var>Stringlist</var>. <code>%pos</code>, 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 <var class="term">regex</var> 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 <var class="term">string</var> contained multiple numbers separated by arithmetic operators, you could use the <var>[[RegexSplit (Stringlist function)|RegexSplit]]</var> method to apply the <var class="term">regex</var> repeatedly to the <var class="term">string</var> and collect in a <var>Stringlist</var> the numbers that were separated by the operators.


==See also==
==See also==
{{Template:Stringlist:RegexCapture footer}}
{{Template:Stringlist:RegexCapture footer}}
[[Category:Regular expression processing]]

Latest revision as of 22:17, 21 January 2022

Capture substrings to Stringlist using regex (Stringlist class)


This callable 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").

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:
>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 request 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 regular expression processing demands on PDL and STBL space by setting, say, UTABLE LPDLST 3000 and UTABLE LSTBL 9000. See SOUL 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.

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 method to apply the regex repeatedly to the string and collect in a Stringlist the numbers that were separated by the operators.

    See also