Function: RegexSplit
Function: RegexSplit

Function: RegexSplit

RegexSplit ( REGEX, STRING )

Splits a string into multiple substrings using a RegEx pattern as a delimiter.

Parameters

Parameter Type Description
REGEX VALUE(STRING) The RegEx pattern to use.
STRING VALUE(STRING) The string to split.

Returns

VALUE(LIST) The list of substrings returned after splitting.

What It Does

This function splits a string into a list of substrings by splitting it up at the boundaries of each match of the provided RegEx pattern. Both parameters are interpreted as strings before the split.

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().

Example

RegEx Function Example


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;
    }
}

Additional Technical Notes

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.

×

Modal Header