RegexMatch (String function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (match syntax diagram to revised template; fix tags and links)
Line 1: Line 1:
{{Template:String:RegexMatch subtitle}}
{{Template:String:RegexMatch subtitle}}


This [[Intrinsic classes|intrinsic]] function determines whether a given pattern (regular expression,
The <var>RegexMatch</var> [[Intrinsic classes|intrinsic]] function determines whether a given pattern (regular expression, or "regex") matches within a given string according to the "<var>[[Regex_processing#Regex_rules|rules]]</var>" of regular expression matching.
or "regex") matches within a given string according to the "[[Regex rules|rules]]" of regular expression matching.


<var>RegexMatch</var> is available as of version 7.2 of the <var class=product>Sirius Mods</var>.
==Syntax==
==Syntax==
{{Template:String:RegexMatch syntax}}
{{Template:String:RegexMatch syntax}}
===Syntax terms===
===Syntax terms===
<table class="syntaxTable">
<table class="syntaxTable">
<tr><th>%pos</th>
<tr><th>%number</th>
<td>A variable to receive the position of the character '''after'''                                    the last character matched, or a zero if no characters in the method                                   object string match the regular expression.                                                           </td></tr>
<td>A variable to return the position of the character <b><i>after</i></b> the last character matched, or a zero if no characters in the method object string match the regular expression.</td></tr>
<tr><th>string</th>
<tr><th>string</th>
<td>The input string, to which the regular expression ''regex'' 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 method object string                                         to determine whether the regex matches ''string''.                                                     </td></tr>
<td>A string that is interpreted as a regular expression and is applied to the method object <var class="term">string</var> to determine whether the regex matches <var class="term">string</var>.</td></tr>
<tr><th>Options=opts</th>
<tr><th>Options</th>
<td>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 [[Common regex options]].
<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>.
<table class="syntaxNested">
<table class="syntaxNested">
<tr><th>I</th>
<tr><th>I</th>
<td>Do case-insensitive matching between ''string'' and ''regex''.                                     </td></tr>
<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 dot (''''.'''')                                                                   can match any character, including carriage return and linefeed. </td></tr>
<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 ''wherever'' the indicator appears in the input string.                                                                                                   M mode is ignored if C (XML Schema) mode is specified.                             </td></tr>
<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 modex|XML Schema regex rules]].         Each regex is implicitly anchored at the beginning and end,                       and no characters serve as anchors.                                               </td></tr>
<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>
</table></td></tr>
</td></tr>
<tr><th>CaptureList</th><td></td></tr>
</table>
</table>


===Exceptions===
===Exceptions===
 
<var>RegexMatch</var> can throw the following exceptions:
This [[Intrinsic classes|intrinsic]] function can throw the following exceptions:
<dl>
<dl>
<dt>[[InvalidRegex]]
<dt><var>[[InvalidRegex_class|InvalidRegex]]</var>
<dd>If the ''regex'' parameter does not contain a valid regular expression.
<dd>If the <var class="term">regex</var> parameter does not contain a valid regular expression. The exception object indicates the position of the character in the <var class="term">regex</var> parameter where it was determined that the regular expression is invalid, and a description of the nature of the error.
The exception object indicates the position of the character in the ''regex''
parameter where it was determined that the regular expression is invalid, and
a description of the nature of the error.
</dl>
</dl>
==Usage notes==
==Usage notes==
*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 [[User Language programming considerations]].
<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>.
*For information about additional methods that support regular expressions, see [[Regex Processing]].
<li>For information about additional methods that support regular expressions, see <var>[[Regex_processing|Regex Processing]]</var>.
*<var>RegexMatch</var> is something of a misnomer. It does not determine if a string matches a regular expression, it determines if a string '''contains''' a substring that matches a regular expression. <var>RegexMatch</var> behaves more like a matching method if the regular expression is "anchored" (begins with a caret (''''&circ.'''') and ends with a dollar sign (''''$''''), or if the C option indicates XML Schema mode.
<li><var>RegexMatch</var> is something of a misnomer. It does not determine if a string matches a regular expression, it determines if a string <b><i>contains</i></b> a substring that matches a regular expression. <var>RegexMatch</var> behaves more like a matching method if the regular expression is "anchored" (begins with a caret ('<code>&circ;</code>') and ends with a dollar sign ('<code>$</code>')), or if the C option indicates XML Schema mode.
<li><var>RegexMatch</var> is available as of <var class="product">[[Sirius Mods]]</var> Version 7.2.</ul>
 
==Examples==
==Examples==
 
<ol><li>The following example tests whether the regex <b>'\*bc?[5-8]'</b> contains a substring that matches <b>'a*b6'</b>.
The following example tests whether the regex ''''\*bc?[5-8]''''
contains a substring that matches ''''a*b6''''.
<p class="code">begin
<p class="code">begin
  %rc float
  %rc float
  %regex <var>Longstring</var>
  %regex longstring
  %string <var>Longstring</var>
  %string longstring
 
  %regex = '\*bc?[5-8]'
  %regex = '\*bc?[5-8]'
  %string = 'a\*b6'
  %string = 'a\*b6'


  %rc = %string:regexmatch(%regex)
  %rc = %string:regexmatch(%regex)
  if %rc then
  if %rc then
    [[Intrinsic classes#printtext|printText]] '{%regex}' matches '{%string}'
      printText '{%regex}' matches '{%string}'
  else
  else
    printText '{%regex}' does not match '{%string}'
      printText '{%regex}' does not match '{%string}'
   end if
   end if
end
end
</p>
</p>
The regex matches the input string; the example result is:
The regex matches the input string; the example result is:
<p class="output">'\*bc?[5-8]' matches 'a\*b6'
<p class="output">'\*bc?[5-8]' matches 'a\*b6'
</p>
</p>
This regex demonstrates the following:
This regex demonstrates the following:
*To match a string, a regex pattern must merely "fit" a substring of the string.
<ul><li>To match a string, a regex pattern must merely "fit" a substring of the string.
*Metacharacters, in this case star (''''*''''), must be escaped.
<li>Metacharacters, in this case star ('<code>*</code>'), must be escaped.
*An optional character (''''c?'''') may fail to find a match, but this does not prevent the success of the overall match.
<li>An optional character ('<code>c?</code>') may fail to find a match, but this does not prevent the success of the overall match.
*The character class range (''''[5-8]'''') matches the ''''6'''' in the input string.
<li>The character class range ('<code>[5-8]</code>') matches the '<code>6</code>' in the input string.
</ul></ol>


==See also==
==See also==
<ul><li>For details of the <var>printtext</var> statement, please see <var>[[Intrinsic classes#printtext|printText]]</var></ul>
{{Template:String:RegexMatch footer}}
{{Template:String:RegexMatch footer}}

Revision as of 02:10, 3 February 2011

Position after match of regex (String class)


The RegexMatch intrinsic function determines whether a given pattern (regular expression, or "regex") matches within a given string according to the "rules" of regular expression matching.

Syntax

%number = string:RegexMatch( regex, [Options= string], - [CaptureList= stringlist]) Throws InvalidRegex

Syntax terms

%number A variable to return the position of the character after the last character matched, or a zero if no characters in the method object string match the regular expression.
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 method object string to determine whether the regex matches string.
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.
I Do case-insensitive matching between string and regex.
S Dot-All mode: a period (.) 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.
CaptureList

Exceptions

RegexMatch can throw the following exceptions:

InvalidRegex
If the regex parameter does not contain a valid regular expression. The exception object indicates the position of the character in the regex parameter where it was determined that the regular expression is invalid, and a description of the nature of the error.

Usage notes

  • 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 User Language programming considerations.
  • For information about additional methods that support regular expressions, see Regex Processing.
  • RegexMatch is something of a misnomer. It does not determine if a string matches a regular expression, it determines if a string contains a substring that matches a regular expression. RegexMatch behaves more like a matching method if the regular expression is "anchored" (begins with a caret ('ˆ') and ends with a dollar sign ('$')), or if the C option indicates XML Schema mode.
  • RegexMatch is available as of Sirius Mods Version 7.2.

Examples

  1. The following example tests whether the regex '\*bc?[5-8]' contains a substring that matches 'a*b6'.

    begin %rc float %regex longstring %string longstring %regex = '\*bc?[5-8]' %string = 'a\*b6' %rc = %string:regexmatch(%regex) if %rc then printText '{%regex}' matches '{%string}' else printText '{%regex}' does not match '{%string}' end if end

    The regex matches the input string; the example result is:

    '\*bc?[5-8]' matches 'a\*b6'

    This regex demonstrates the following:

    • To match a string, a regex pattern must merely "fit" a substring of the string.
    • Metacharacters, in this case star ('*'), must be escaped.
    • An optional character ('c?') may fail to find a match, but this does not prevent the success of the overall match.
    • The character class range ('[5-8]') matches the '6' in the input string.

See also

  • For details of the printtext statement, please see printText