RegexMatch (String function): Difference between revisions
m (→Syntax terms) |
m (→Syntax terms) |
||
Line 38: | Line 38: | ||
<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>. | <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>For information about additional methods that support regular expressions, see <var>[[Regex_processing|"Regex Processing"]]</var>. | <li>For information about additional methods that support regular expressions, see <var>[[Regex_processing|"Regex Processing"]]</var>. | ||
<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 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>&amp;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| | <li><var>RegexMatch</var> is available as of <var class="product">[[Sirius Mods|Sirius Mods]]</var> Version 7.2.</ul> | ||
==Examples== | ==Examples== |
Revision as of 17:46, 4 May 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".
| ||||||||
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
andUTABLE 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 ('
&circ;
') 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
- 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.