IsRegEx() returns whether the specified text is a valid regular expression.
This function performs regex operations that are case-sensitive. You can alternatively use the variant IsRegExI() to perform case-insensitive regex operations.
Syntax:
IsRegEx
(expr [, debug])
Return data type: dual
Arguments
Argument
Description
expr
String expression containing the regular expression to evaluate.
debug
Optional argument. If any value is specified for this argument, the expression returns a text hint outlining how an invalid regular expression might be fixed.
Function examples
Example
Result
IsRegEx('[a-z]')
Returns -1 (true).
IsRegEx(']0-1[', 1)
This example returns an error message with a hint to correct the invalid regular expression ]0-1[.
IsRegEx('[a-z')
Returns 0 (false). No error message is shown because the debug argument is not specified.
When to use it
You can use IsRegEx() to test whether the regular expressions you write are valid in Qlik Sense. For example, you can load a table into your app with input text and an evaluation of the text's validity as regex.
Example 1 – loading validity of regex into data model
Overview
Open the Data load editor and add the load script below to a new section.
The load script contains:
A field, reg_exp, containing input strings that may or may not be valid regular expressions.
Two calculated fields to check if each value in reg_exp is a valid regular expression or not:
validity_no_hint: Displays -1 (true) if the regex is valid, and 0 (false) if it is invalid.
validity_with_hint: Displays -1 (true) if the regex is valid. If the regex is invalid, an autogenerated error message outlines issues with the input, with possible hints to help fix it.
Load the data and open a sheet. Create a new table and add these fields as dimensions:
reg_exp
validity_no_hint
validity_with_hint
Results table
reg_exp
validity_no_hint
validity_with_hint
([a-z)(0-9)+
0
-1
[a-z]+
-1
Hint text (autogenerated)
\$|¢(0-9)
-1
Hint text (autogenerated)
abcdefg|[0-7
0
-1
Example 2 – only loading valid regex into data model
Overview
Open the Data load editor and add the load script below to a new section.
The load script contains:
A field, reg_exp, containing input strings that may or may not be valid regular expressions. This field is contained in a table named Regular Expressions.
A where clause that only loads records into the data model when the input is a valid regular expression.
Load the data and open a sheet. Create a new table and add this field as a dimension:
reg_exp
Results table
reg_exp
[a-z]+
\$|¢(0-9)
[a-z]+ and \$|¢(0-9) are valid regular expressions, so they have been loaded into the data model. ([a-z)(0-9)+ and abcdefg|[0-7 are not valid regular expressions, so they are not loaded into the data model.
Example 3 – chart expressions
Overview
In this example, we load a set of input strings into the data model and add them to a table, along with two calculated dimensions which evaluate the validity of the regex. The two calculated dimensions convey the same information in different ways.
Open the Data load editor and add the following load script to a new section.
The load script contains a field, reg_exp, containing input strings that may or may not be valid regular expressions. This field is contained in a table named Regular Expressions.