$RegexReplace: Difference between revisions
(Automatically generated page update) |
No edit summary |
||
Line 29: | Line 29: | ||
Except when the <tt>A</tt> option is specified (as described below for the <var class="term">options</var> argument), you can include markers in the <var class="term">replacement</var> value to indicate where to insert corresponding captured strings — strings matched by capturing groups (parenthesized subexpressions) in <var class="term">regex</var>, if any. | Except when the <tt>A</tt> option is specified (as described below for the <var class="term">options</var> argument), you can include markers in the <var class="term">replacement</var> value to indicate where to insert corresponding captured strings — strings matched by capturing groups (parenthesized subexpressions) in <var class="term">regex</var>, if any. | ||
As in Perl, these markers are in the form <code>$n</code>, where | As in Perl, these markers are in the form <code>$n</code>, where <i>n</i> is the number of the capture group, and 1 is the number of the first capture group. <i>n</i> must not be 0 or contain more than 9 digits. | ||
If a capturing group makes no matches (is positional, for example), or if there was no | If a capturing group makes no matches (is positional, for example), or if there was no <i>n</i>th capture group corresponding to the <tt>$n</tt> marker in a replacement string, the value of <tt>$n</tt> used in the replacement string is the empty string. | ||
<code>xxx$1</code> is an example of a valid replacement string, and <code>$0yyy</code> is an example of a | <code>xxx$1</code> is an example of a valid replacement string, and <code>$0yyy</code> is an example of a <strong>non-valid</strong> one. | ||
Or you can use the format <code>$mn</code>, where | Or you can use the format <code>$mn</code>, where <i>m</i> is one of the following modifiers: | ||
<table class="syntaxTable"> | <table class="syntaxTable"> | ||
<tr><th><var>U</var> or <var class="camel">u</var></th> | <tr><th><var>U</var> or <var class="camel">u</var></th> | ||
Line 58: | Line 58: | ||
<tr><th><var>M</var></th> | <tr><th><var>M</var></th> | ||
<td>Multi-line mode: let anchor characters match end-of-line indicators | <td>Multi-line mode: let anchor characters match end-of-line indicators <strong>wherever</strong> 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> | <tr><th><var>C</var></th> | ||
Line 67: | Line 67: | ||
<tr><th><var>A</var></th> | <tr><th><var>A</var></th> | ||
<td>Copy the <var class="term">replacement</var> string as is. Do not recognize escapes; interpret a <code>$n</code> combination as a literal and | <td>Copy the <var class="term">replacement</var> string as is. Do not recognize escapes; interpret a <code>$n</code> combination as a literal and <strong>not</strong> as a special marker; and so on.</td></tr> | ||
</table></td></tr> | </table></td></tr> | ||
Line 87: | Line 87: | ||
</table> | </table> | ||
<p class="note"><b>Note:</b> If you omit this argument and a negative <var class="term">%status</var> value is to be returned, the run is cancelled.</p></td></tr> | |||
</table> | </table> | ||
Line 95: | Line 95: | ||
<li>It is strongly recommended that you protect your environment from regex processing demands on PDL and STBL space by setting, say, <tt>UTABLE LPDLST 3000</tt> and <tt>UTABLE LSTBL 9000</tt>. For further discussion of this, | <li>It is strongly recommended that you protect your environment from regex processing demands on PDL and STBL space by setting, say, <tt>UTABLE LPDLST 3000</tt> and <tt>UTABLE LSTBL 9000</tt>. For further discussion of this, | ||
<li>$RegexReplace is Longstring-capable. Its string inputs and outputs are considered Longstrings for expression-compilation purposes, and they have standard Longstring truncation behavior: truncation by assignment results in request cancellation. For more information, | <li>$RegexReplace is Longstring-capable. Its string inputs and outputs are considered Longstrings for expression-compilation purposes, and they have standard Longstring truncation behavior: truncation by assignment results in request cancellation. For more information, | ||
<li>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 | <li>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 <strong>not</strong> followed by a question mark (<tt>?</tt>). A capturing group that is nested within a non-capturing subexpression is still a capturing group. | ||
<li>In Perl, <tt>$n</tt> markers (<tt>$1</tt>, for example) enclosed in single quotes are treated as literals instead of as "that which was captured by the first capturing parentheses." <var>$RegexReplace</var> uses the <tt>A</tt> option of the Option argument for this purpose. | <li>In Perl, <tt>$n</tt> markers (<tt>$1</tt>, for example) enclosed in single quotes are treated as literals instead of as "that which was captured by the first capturing parentheses." <var>$RegexReplace</var> uses the <tt>A</tt> option of the Option argument for this purpose. | ||
<li>A regex may "succeed" but match no characters. For example, a quantifier like <tt>?</tt> is allowed by definition to match no characters, though it tries to match one. <var>$RegexReplace</var> honors such a zero-length match by substituting the specified replacement string at the current position. If the global option is in effect, the regex is then applied again one position to the right in the input string, and again, until the end of the string. The regex <tt>9?</tt> globally applied to the string <tt>abc</tt> with a comma-comma (<tt>,,</tt>) replacement string results in this output string: <code>,,a,,b,,c,,</code>. | <li>A regex may "succeed" but match no characters. For example, a quantifier like <tt>?</tt> is allowed by definition to match no characters, though it tries to match one. <var>$RegexReplace</var> honors such a zero-length match by substituting the specified replacement string at the current position. If the global option is in effect, the regex is then applied again one position to the right in the input string, and again, until the end of the string. The regex <tt>9?</tt> globally applied to the string <tt>abc</tt> with a comma-comma (<tt>,,</tt>) replacement string results in this output string: <code>,,a,,b,,c,,</code>. | ||
Line 128: | Line 128: | ||
==Examples== | ==Examples== | ||
In the following example, the regex <code>(5.)</code> is applied repeatedly (global option) to the string <code>5A5B5C5D5E</code> to replace the uppercase letters with their lowercase counterparts. The <tt>$L1</tt> | In the following example, the regex <code>(5.)</code> is applied repeatedly (global option) to the string <code>5A5B5C5D5E</code> to replace the uppercase letters with their lowercase counterparts. The <tt>$L1</tt> <strong>%replacement</strong> value makes the replacement string equal to whatever is matched by the capturing group, <tt>(5.)</tt>, in the regex (the <tt>L</tt> causes the lowercase versions of the captured letters to be used). | ||
<p class="code"> Begin | <p class="code"> Begin |
Revision as of 23:06, 11 November 2014
Replace matching strings
Note: Most Sirius $functions have been deprecated in favor of Object Oriented methods. The OO equivalent for the $RegexReplace function is the RegexReplace String function.
This function searches a given string for matches of a regular expression, and it replaces found matches with or according to a specified replacement string. The function stops after the first match and replace, or it can continue searching and replacing until no more matches are found. The function is available as of Version 6.9 of the Sirius Mods.
Matches are obtained according to the "rules" of regular expression matching (information about the rules observed is provided in ).
$RegexReplace accepts three required and two optional arguments, and it returns a string. It is also callable. Specifying an invalid argument results in request cancellation.
Syntax
outStr = $RegexReplace(inStr, regex, replacement, [options], [%status])
Syntax terms
outStr | A string set to the value of inStr with each matched substring replaced by the value of replacement. | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
inStr | the input string, to which the regular expression regex is applied. This is a required argument. | ||||||||||||
regex | a string that is interpreted as a regular expression and that is applied to the inStr argument to find the one or more inStr substrings matched by regex. This is a required argument. | ||||||||||||
replacement | The string that replaces the substrings of inStr that regex matches. This is a required argument.
Except when the A option is specified (as described below for the options argument), you can include markers in the replacement value to indicate where to insert corresponding captured strings — strings matched by capturing groups (parenthesized subexpressions) in regex, if any. As in Perl, these markers are in the form If a capturing group makes no matches (is positional, for example), or if there was no nth capture group corresponding to the $n marker in a replacement string, the value of $n used in the replacement string is the empty string.
The only characters you can escape in a replacement string are dollar sign ($), backslash (\), and the digits 0 through 9. So only these escapes are respected: .\\, \$, and \0 through \9. No other escapes are allowed in a replacement string — this includes "shorthand" escapes like \d — and an "unaccompanied" backslash (\) is an error. For example, since the scan for the number that accompanies the meta-$ stops at the first non-numeric, you use 1$1\2 to indicate that the first captured string should go between the numbers 1 and 2 in the replacement string. | ||||||||||||
options | 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".
| ||||||||||||
%status | An optional, integer status code. These values are possible:
Note: If you omit this argument and a negative %status value is to be returned, the run is cancelled. |
Usage notes
- 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,
- $RegexReplace is Longstring-capable. Its string inputs and outputs are considered Longstrings for expression-compilation purposes, and they have standard Longstring truncation behavior: truncation by assignment results in request cancellation. For more information,
- 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 (?). A capturing group that is nested within a non-capturing subexpression is still a capturing group.
- In Perl, $n markers ($1, for example) enclosed in single quotes are treated as literals instead of as "that which was captured by the first capturing parentheses." $RegexReplace uses the A option of the Option argument for this purpose.
- A regex may "succeed" but match no characters. For example, a quantifier like ? is allowed by definition to match no characters, though it tries to match one. $RegexReplace honors such a zero-length match by substituting the specified replacement string at the current position. If the global option is in effect, the regex is then applied again one position to the right in the input string, and again, until the end of the string. The regex 9? globally applied to the string abc with a comma-comma (,,) replacement string results in this output string:
,,a,,b,,c,,
. - Say you want to supply end tags to items of of the form <img foo="bar">, converting them to <img foo="bar"></img>. You decide to use the following regex to capture img tags that have attributes:
(<img .*>)
And you use the following replacement string to replace the captured string with the captured string plus an appended
</img>
:$1</img>
However, if the regex above is applied to the string <body><img src="foo" width="24"></body>, the end tag </img> is not inserted after the first closing angle bracket (>) after "24" as you want. Instead, the matched string greedily extends to the second closing angle bracket, and the tag </img> is positioned at the end:
<body><img src="foo" width="24"></body></img>
One remedy for this situation is to use the following regex, which employs a negated character class to match non-closing-bracket characters:
(<img [ˆ>]*>)
This regex does not extend beyond the first closing angle bracket in the target input string, and the resulting output string is:
<body><img src="foo" width="24"></img></body>
- For information about additional methods and $functions that support regular expressions, see "Regex processing".
- This $function is available as of Version 6.9.
Examples
In the following example, the regex (5.)
is applied repeatedly (global option) to the string 5A5B5C5D5E
to replace the uppercase letters with their lowercase counterparts. The $L1 %replacement value makes the replacement string equal to whatever is matched by the capturing group, (5.), in the regex (the L causes the lowercase versions of the captured letters to be used).
Begin %regex Longstring %inStr Longstring %replacement Longstring %outStr Longstring %opt string len 10 %status float %inStr='5A5B5C5D5E' %regex='(5.)' %replacement='$L1' %opt='g' %outStr = $RegexReplace (%inStr, %regex, %replacement, - %opt, %status) Print '%RegexReplace: status = ' %status Print 'OutputString: ' %outStr End
The example result is:
%RegexReplace: status = 5 OutputString: 5a5b5c5d5e
The non-capturing regex 5. matches and replaces the same substrings as the capturing group (5.), but (5.) is used above to take advantage of the self-referring marker for the replacement string, $L1, which is valid only for capturing groups.
Products authorizing $RegexReplace