Parameter | Type | Description |
---|---|---|
REGEX | VALUE(STRING) | The RegEx pattern to use. |
STRING | VALUE(STRING) | The string to scan. |
VALUE(INTEGER) | The index of the first match or -1 if not found. |
This function uses the provided RegEx pattern to find the first matching substring in the provided input string. This returns the starting index of the match if a matching substring is found, and -1 if nothing was matched. Both parameters are interpreted as strings before the scan.
If the RegEx pattern provided is an invalid or unparseable expression, this function will throw a FATAL cue and halt the module. To avoid this, you may want to pre-check the validity of the expressions you use with IsRegex().
world
{
start()
{
textln(isRegex(`asdf\s\+`)); // true
textln(isRegex(`\`)); // false
textln("");
s = "Apples and Oranges! 12345.67890";
textln(regexFind(`[A-Za-z]+`, s)); // 0
textln(regexFind(`[0-9]+`, s)); // 20
textln("");
textln(regexFindLast(`[A-Za-z]+`, s)); // 11
textln(regexFindLast(`[0-9]+`, s)); // 26
textln("");
textln(regexGet(`[A-Za-z]+`, s)); // "Apples"
textln(regexGet(`[0-9]+`, s)); // "12345"
textln("");
textln(regexGetLast(`[A-Za-z]+`, s)); // "Oranges"
textln(regexGetLast(`[0-9]+`, s)); // "67890"
textln("");
textln(regexGetAll(`[A-Za-z]+`, s)); // ["Apples", "and", "Oranges"]
textln(regexGetAll(`[0-9]+`, s)); // ["12345", "67890"]
textln("");
textln(regexMatches(`[A-Za-z]+`, s)); // false
textln(regexMatches(`[A-Za-z]+`, "Apples")); // true
textln("");
textln(regexSplit(`[\s]+`, s)); // ["Apples", "and", "Oranges!", "12345.67890"]
textln(regexSplit(`[\s!\.]+`, s)); // ["Apples", "and", "Oranges", "12345", "67890"]
textln("");
textln(regexReplace(`[\s]+`, "", s)); // "ApplesandOranges!12345.67890"
textln(regexReplace(`[\s!\.]+`, "==", s)); // "Apples==and==Oranges==12345==67890"
quit;
}
}
Note that when passing a literal string to this function, the backslash \ character is important in RegEx, so any use of it as part of the expression should be escaped: \\. For this reason, it may be better for readability's sake to use raw strings, like in the example above.
All RegExes in TAME are considered case-sensitive and match globally (the entirety of the input string used with it is potentially examined).
There are several resources on RegEx out there, such as www.regular-expressions.info and and a more interactive example at RegExr.com.