Create a regular expression pattern to match a LIKE mask


LOADLIB "wh::regex.whlib";

STRING FUNCTION CreateLikeRegexPattern(STRING likemask)


STRING likemask

The LIKE mask to match

Return value


The regular expression pattern


This function can be used to create a regular expression to perform a LIKE match. This function allows for "" and "?" to be escaped as "*" and "?", so the literal characters "" and "?" can be matched (which is not possible using LIKE). The resulting pattern can also be used to capture the matched wildcard values.


STRING string1 := "Is this a test?";
STRING string2 := "Is this a test!";

// Returns TRUE
BOOLEAN like1 := string1 LIKE "Is * a test?";
// Also returns TRUE (The "?" matches a single character)
BOOLEAN like2 := string2 LIKE "Is * a test?";

// Using "\\?" to escape the "?", so we can match the "?" character itself
STRING pattern := CreateLikeRegexPattern("Is * a test\\?");
OBJECT expression := NEW RegEx(pattern);

// Returns TRUE
BOOLEAN test1 := expression->Test(string1);
// Returns FALSE
BOOLEAN test2 := expression->Test(string2);

// exec1 contains two records, the first containing the complete string, the second containing "this" (matched by the "*")
RECORD ARRAY exec1 := expression->Exec(string1);
// exec2 is a default record array, because it doesn't match
RECORD ARRAY exec2 := expression->Exec(string2);