Regex functions
Functions exposed through regex builtin object.
If your regular expression contains backslashes (\), you will need to do one of the following:
- Anywhere you would use a
\, use two. For example:"\d+\.\d+"becomes"\\d+\\.\\d+" - Use verbatim strings. For example:
"\d+\.\d+"becomes`\d+\.\d+`
regex.escape
regex.escape <pattern>Description
Escapes a minimal set of characters (\, *, +, ?, |, {, [, (,), ^, $,., #, and white space)
by replacing them with their escape codes.
This instructs the regular expression engine to interpret these characters literally rather than as metacharacters.
Arguments
pattern: The input string that contains the text to convert.
Returns
A string of characters with metacharacters converted to their escaped form.
Examples
input
{{ "(abc.*)" | regex.escape }}output
\(abc\.\*\)regex.match
regex.match <text> <pattern> <options>?Description
Searches an input string for a substring that matches a regular expression pattern and returns an array with the match occurences.
Arguments
text: The string to search for a match.pattern: The regular expression pattern to match.options: A string with regex options, that can contain the following option characters (default isnull): -i: Specifies case-insensitive matching. -m: Multiline mode. Changes the meaning of^and$so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string. -s: Specifies single-line mode. Changes the meaning of the dot.so it matches every character (instead of every character except\n). -x: Eliminates unescaped white space from the pattern and enables comments marked with#.
Returns
An array that contains all the match groups. The first group contains the entire match. The other elements contain regex matched groups (..). An empty array returned means no match.
Examples
input
{{ "this is a text123" | regex.match `(\w+) a ([a-z]+\d+)` }}output
["is a text123", "is", "text123"]Notice that the first element returned in the array is the entire regex match, followed by the regex group matches.
regex.matches
regex.matches <text> <pattern> <options>?Description
Searches an input string for multiple substrings that matches a regular expression pattern and returns an array with the match occurences.
Arguments
text: The string to search for a match.pattern: The regular expression pattern to match.options: A string with regex options, that can contain the following option characters (default isnull): -i: Specifies case-insensitive matching. -m: Multiline mode. Changes the meaning of^and$so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string. -s: Specifies single-line mode. Changes the meaning of the dot.so it matches every character (instead of every character except\n). -x: Eliminates unescaped white space from the pattern and enables comments marked with#.
Returns
An array of matches that contains all the match groups. The first group contains the entire match. The other elements contain regex matched groups (..). An empty array returned means no match.
Examples
input
{{ "this is a text123" | regex.matches `(\w+)` }}output
[["this", "this"], ["is", "is"], ["a", "a"], ["text123", "text123"]]Notice that the first element returned in the sub array is the entire regex match, followed by the regex group matches.
regex.replace
regex.replace <text> <pattern> <replace> <options>?Description
In a specified input string, replaces strings that match a regular expression pattern with a specified replacement string.
Arguments
text: The string to search for a match.pattern: The regular expression pattern to match.replace: The replacement string.options: A string with regex options, that can contain the following option characters (default isnull): -i: Specifies case-insensitive matching. -m: Multiline mode. Changes the meaning of^and$so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string. -s: Specifies single-line mode. Changes the meaning of the dot.so it matches every character (instead of every character except\n). -x: Eliminates unescaped white space from the pattern and enables comments marked with#.
Returns
A new string that is identical to the input string, except that the replacement string takes the place of each matched string. If pattern is not matched in the current instance, the method returns the current instance unchanged.
Examples
input
{{ "abbbbcccd" | regex.replace "b+c+" "-Yo-" }}output
a-Yo-dregex.split
regex.split <text> <pattern> <options>?Description
Splits an input string into an array of substrings at the positions defined by a regular expression match.
Arguments
text: The string to split.pattern: The regular expression pattern to match.options: A string with regex options, that can contain the following option characters (default isnull): -i: Specifies case-insensitive matching. -m: Multiline mode. Changes the meaning of^and$so they match at the beginning and end, respectively, of any line, and not just the beginning and end of the entire string. -s: Specifies single-line mode. Changes the meaning of the dot.so it matches every character (instead of every character except\n). -x: Eliminates unescaped white space from the pattern and enables comments marked with#.
Returns
A string array.
Examples
input
{{ "a, b , c, d" | regex.split `\s*,\s*` }}output
["a", "b", "c", "d"]regex.unescape
regex.unescape <pattern>Description
Converts any escaped characters in the input string.
Arguments
pattern: The input string containing the text to convert.
Returns
A string of characters with any escaped characters converted to their unescaped form.
Examples
input
{{ "\\(abc\\.\\*\\)" | regex.unescape }}output
(abc.*)