RegexReplaceCorresponding (Stringlist function): Difference between revisions
m (reviewed and some tags) |
m (found lots of regex related links) |
||
Line 1: | Line 1: | ||
{{Template:Stringlist:RegexReplaceCorresponding subtitle}} | {{Template:Stringlist:RegexReplaceCorresponding subtitle}} | ||
<p>This method searches a given string for matches to one of multiple regular expressions contained in a list, and it replaces found matches with or according to a string contained in a list that corresponds to the regex list.</p><p>The regex list items are treated as mutually exclusive alternatives, and the function stops as soon as an item matches and the replacement is made. A "global" option is also available to continue searching and replacing within the given string using the matching regex item until no more matches are found.</p><p><var>RegexReplaceCorresponding</var> uses the rules of regular expression matching (information about which is provided in [[Regex processing]]).</p><p><var>RegexReplaceCorresponding</var> accepts two required and two optional arguments, and it returns a string.</p> | <p>This method searches a given string for matches to one of multiple regular expressions contained in a list, and it replaces found matches with or according to a string contained in a list that corresponds to the regex list.</p><p>The regex list items are treated as mutually exclusive alternatives, and the function stops as soon as an item matches and the replacement is made. A "global" option is also available to continue searching and replacing within the given string using the matching regex item until no more matches are found.</p><p><var>RegexReplaceCorresponding</var> uses the rules of regular expression matching (information about which is provided in <var>[[Regex_processing#Regex_rules|Regex processing rules]]</var>).</p><p><var>RegexReplaceCorresponding</var> accepts two required and two optional arguments, and it returns a string.</p> | ||
==Syntax== | ==Syntax== | ||
Line 20: | Line 20: | ||
<td>Indicates that the matched substring should be lowercased when inserted.</td></tr> | <td>Indicates that the matched substring should be lowercased when inserted.</td></tr> | ||
</table> | </table> | ||
<p>The only characters you can escape in a replacement string are dollar sign (<code>$</code>), backslash (<code>\</code>), and the digits <code>0</code> through <code>9</code>. So only these escapes are respected:<code>\\</code>, <code>\$</code>, and <code>\0</code> through <code>\9</code>. No other escapes are allowed in a replacement string -- this includes "shorthand" escapes like <code>\d</code> -- and an "unaccompanied" backslash (<code>\</code>) is an error.</p><p>For example, since the scan for the number that accompanies the meta-$ stops at the first nonnumeric, you use <code>1$0\0</code> to indicate that the first matched substring should go between the numbers <code>1</code> and <code>0</code> in the replacement string.</p></td></tr> | <p>The only characters you can escape in a replacement string are dollar sign (<code>$</code>), backslash (<code>\</code>), and the digits <code>0</code> through <code>9</code>. So only these escapes are respected:<code>\\</code>, <code>\$</code>, and <code>\0</code> through <code>\9</code>. No other escapes are allowed in a replacement string -- this includes "shorthand" escapes like <code>\d</code> -- and an "unaccompanied" backslash (<code>\</code>) is an error.</p><p>For example, since the scan for the number that accompanies the meta-$ stops at the first nonnumeric, you use <code>1$0\0</code> to indicate that the first matched substring should go between the numbers <code>1</code> and <code>0</code> in the replacement string.</p></td></tr><tr><th>Options</th> | ||
<tr><th | <td>This is an optional, but <var class="term">nameRequired</var>, 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> | |||
<table class="syntaxNested"> | <table class="syntaxNested"> | ||
<tr><th>I</th> | <tr><th>I</th> | ||
<td>Do case-insensitive matching between <var class="term"> | <td>Do case-insensitive matching between <var class="term">string</var> and <var class="term">regex</var>.</td></tr> | ||
<tr><th>S</th> | <tr><th>S</th> | ||
<td>Dot-All mode: a | <td>Dot-All mode: a period (<code>.</code>) can match any character, including carriage return and linefeed.</td></tr> | ||
<tr><th>M</th> | <tr><th>M</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. < | <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 class="term">M</var> mode is ignored if <var class="term">C</var> (XML Schema) mode is specified.</td></tr> | ||
<tr><th>C</th> | <tr><th>C</th> | ||
<td>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 | <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> | ||
<tr><th>G</th> | <tr><th>G</th> | ||
<td>Replace every occurrence of the match, not just (as in non-< | <td>Replace every occurrence of the match, not just (as in non-<var class="term">G</var> mode) the first matched substring only.</td></tr> | ||
<tr><th>A</th> | <tr><th>A</th> | ||
<td>Copy the <i>replacement</i> string as is. Do not recognize escapes; interpret a < | <td>Copy the <i>replacement</i> string as is. Do not recognize escapes; interpret a <var class="term">$n</var> combination as a literal and <b><i>not</i></b> as a special marker; and so on.</td></tr> | ||
</table> | </table></td></tr> | ||
</td></tr> | |||
<tr><th><b>Status</b></th> | <tr><th><b>Status</b></th> | ||
<td>The <var class="term">Status</var> argument (name required) is optional; if specified, it is set to an integer code. These values are possible: | <td>The <var class="term">Status</var> argument (name required) is optional; if specified, it is set to an integer code. These values are possible: | ||
Line 56: | Line 54: | ||
==Usage notes== | ==Usage notes== | ||
<ul><li>All errors in <var class="term">RegexReplaceCorresponding</var>, including invalid argument(s) result in request cancellation.<li>It is strongly recommended that you protect your environment from | <ul><li>All errors in <var class="term">RegexReplaceCorresponding</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 <var>[[Regex_processing#User_Language_programming_considerations|User Language programming considerations]]</var>. | |||
<li>Items in <var class="term">sl</var> must <b><i>not</i></b> exceed 6124 bytes. However, the <var class="term">inString</var> value and items in <var class="term">replacementList</var> may exceed 6124 bytes.<li>For information about additional methods and $functions that support regular expressions, see [[Regex processing]].<li><var class="term">RegexReplaceCorresponding</var> is available as of <var class="product">Sirius Mods</var> Version 6.9.</ul> | |||
==Examples== | ==Examples== |
Revision as of 01:56, 3 February 2011
Replace substrings that match regex with items in a Stringlist (Stringlist class)
This method searches a given string for matches to one of multiple regular expressions contained in a list, and it replaces found matches with or according to a string contained in a list that corresponds to the regex list.
The regex list items are treated as mutually exclusive alternatives, and the function stops as soon as an item matches and the replacement is made. A "global" option is also available to continue searching and replacing within the given string using the matching regex item until no more matches are found.
RegexReplaceCorresponding uses the rules of regular expression matching (information about which is provided in Regex processing rules).
RegexReplaceCorresponding accepts two required and two optional arguments, and it returns a string.
Syntax
%outString = sl:RegexReplaceCorresponding( inString, replacementList, - [Options= string], - [Status= %output]) Throws InvalidRegex
Syntax terms
outString | A string set to the value of inString with each matched substring replaced by the value of the replacementList item that corresponds to the matching sl item. | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
sl | A Stringlist object whose items are interpreted as regular expressions and applied to the inString value. | ||||||||||||
inString | The input string, to which the regular expressions in sl are applied. | ||||||||||||
replacementList | A Stringlist, each of whose items is a potential replacement string for the substring of inString that is matched by the corresponding item of sl. Except when the
Any character after the dollar sign other than a zero is an error. Multiple zeroes (as many as 9) are permitted; a digit following such a string of zeroes must be escaped. You can also use the format
The only characters you can escape in a replacement string are dollar sign ( For example, since the scan for the number that accompanies the meta-$ stops at the first nonnumeric, you use | ||||||||||||
Options | This is an optional, but nameRequired, 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 RegexReplaceCorresponding, 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. - Items in sl must not exceed 6124 bytes. However, the inString value and items in replacementList may exceed 6124 bytes.
- For information about additional methods and $functions that support regular expressions, see Regex processing.
- RegexReplaceCorresponding is available as of Sirius Mods Version 6.9.
Examples
- In the following code fragment, the second item in regex list
%regList
is the first to match the input stringinStr
. The subexpression in that item performs no special capturing function -- the parentheses are for grouping only. Since%opt='g'
is specified, three replacements are made (using the corresponding, second, item in%repList
):... %regList = new text to %regList abcx a(bc?) abcd end text %repList = new text to %repList & && &&& end text %inStr = 'abc1abc2abcd' %opt='g' %outStr = %regList:RegexReplaceCorresponding(%inStr, %repList, Options=%opt, Status=%st) Print 'Status from ReplaceCorresponding is ' %st Print 'Output String: ' %outStr ...
The result would be:
Status from ReplaceCorresponding is 3 Output String: &&1&&2&&d