Function: IsRegex
Function: IsRegex

Function: IsRegex

IsRegex ( STRING )

Checks if the provided string is a valid, compile-able RegEx pattern.

Parameters

Parameter Type Description
STRING VALUE(STRING) The input string.

Returns

VALUE(BOOLEAN) TRUE if the input is valid RegEx, FALSE if not.

What It Does

This function takes a string and attempts to verify it as valid RegEx. Returns TRUE if the input is valid RegEx, FALSE if not.

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