The Regex class implements methods for compiling and executing regular expressions.
The base class of Regex is Object.
| Compile Flags | Flags used to change regex compilation options. |
| Execute Flags | Flags used to change regex execution options. |
Regex.new(String pattern, Number flags)
Constructs a new Regex object.
| Array |
find(String subject, Number offset, Number flags) Searches for the first match of the regexp pattern in subject, starting from offset, subject to flags. |
| Array |
match(String subject, Number offset, Number flags) Searches for the first match of the regexp pattern in subject, starting from offset, subject to flags. |
| Iterator |
matches(String subject, Number flags) Returns an iterator for repeated matching of the pattern in the string subject, subject to flags. |
| Array |
replace(String subject, Object replace, Number max_sub, Number flags) Searches for all matches of the pattern in the string subject and replaces them according to the parameters replace and control. |
| Iterator |
split(String subject, Number flags) Splits subject string into an array of substrings at the positions defined by a regular expression match. |
| String |
to_string() Returns the regular expression string. |
Array find(String subject,
Number offset,
Number flags)
Searches for the first match of the regexp pattern in subject, starting from offset, subject to flags.
Returns on success an array containing the following elements.
Each captured match is represented as an array containing the following values.
Returns nil on failure.
The following example demonstrates usage of this method.
regex = System.Text.Regex.new("(code)(\\*)", System.Text.Regex.ICASE)
res = regex.find("*CODE*CODE*CODE*")
print(res)
Output:
{1,6,{{CODE,1,4},{*,5,1}}}
Array match(String subject,
Number offset,
Number flags)
Searches for the first match of the regexp pattern in subject, starting from offset, subject to flags.
Returns on success all substring matches ("captures"), in the order they appear in the pattern. false is returned for sub-patterns that did not participate in the match. If the pattern specified no captures then the whole matched substring is returned.
Each captured match is represented as an array containing the following values.
Returns nil on failure.
The following example demonstrates usage of this method.
regex = System.Text.Regex.new("(code)(\\*)", System.Text.Regex.ICASE)
res = regex.match("*CODE*CODE*CODE*")
print(res)
Output:
{{CODE,1,4},{*,5,1}}
Iterator match(String subject,
Number flags)
This function is intended for use with the iterator construct. It returns an iterator for repeated matching of the pattern in the string subject, subject to execution flags.
On every iteration (that is, on every match), the iterator returns all captures in the order they appear in the pattern (or the entire match if the pattern specified no captures). The iteration will continue till the subject fails to match.
Each captured match is represented as an array containing the following values.
Returns nil on failure.
The following example demonstrates usage of this method.
regex = System.Text.Regex.new("\\b(\\w+)\\b")
iter = regex.matches("This is one sentence.")
while (iter.has_next()) {
print(iter.next())
}
Output:
{{This,0,4}}
{{is,5,2}}
{{one,8,3}}
{{sentence,12,8}}
Array replace(String subject,
Object replace,
Number control,
Number flags)
This function searches for all matches of the pattern in the string subject and replaces them according to the parameters replace and control.
The parameter replace can be either a String, a Method or a HashMap. On each match made, it is converted into a value replace_out that may be used for the replacement.
Returns on success an array containing the following elements.
Returns nil on failure.
The parameter replace can be either a string, a function or a table. On each match made, it is converted into a value replace_out that may be used for the replacement.
replace_out is generated differently depending on the type of replace:
1. If replace is a string then it is treated as a template for substitution, where the %X occurences in replace are handled in a special way, depending on the value of the character X:
2. If replace is a function then it is called on each match with the submatches passed as parameters (if there are no submatches then the entire match is passed as the only parameter). replace_out is the return value of the replace call, and is interpreted as follows:
3. If replace is a table then replace_out is replace [m1], where m1 is the first submatch (or the entire match if there are no submatches), following the same rules as for the return value of repl call, described in the above paragraph.
replace behaves differently depending on the type of control:
1. If control is a number then it is treated as the maximum number of matches to search for (an omitted or nil value means an unlimited number of matches). On each match, the replacement value is the replace_out string (see above).
2. If control is a function, then it is called on each match, after replace_out is produced (so if repl is a function, it will be called prior to the n call).
n receives 3 arguments and returns 2 values. Its arguments are:
The type of its first return controls the replacement produced by replace for the current match:
The type of its second return controls replace behavior after the current match is handled:
Iterator split(String subject,
Number flags)
This function is used for splitting a subject string subject into parts (sections). The regular expression pattern represents separators between the sections.
The method returns an iterator for repeated matching of the pattern in the string subject, subject to flags.
On every iteration pass, the iterator returns:
The iteration will continue till the end of the subject. Unlike matches, there will always be at least one iteration pass, even if there are no matches in the subject.
Returns nil on failure.
The following example demonstrates usage of this method.
regex = System.Text.Regex.new("code", System.Text.Regex.ICASE)
iter = regex.split("**CODE^^CODE$$CODE%%CODE")
while (iter.has_next()) {
print(iter.next())
}
Output:
{**,CODE,2,4}
{^^,CODE,8,4}
{$$,CODE,14,4}
{%%,CODE,20,4}
String to_string()
This function returns the regular expression string.