Function: RegexReplace
Function: RegexReplace

Function: RegexReplace

RegexReplace ( REGEX, REPLACEMENT, STRING )

Replaces the occurrence of every match of a RegEx pattern with a replacement string, and returns the result.

Parameters

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

Returns

VALUE(STRING) The resultant string after replacement.

What It Does

This function finds each RegEx match and replaces each match with the provided replacement string. All parameters are interpreted as strings before replacement.

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