Page 1 of 1

New :EscapeRegExp Section Variable parameter

Posted: January 5th, 2014, 4:38 pm
by jsmorley
We have added a new :EscapeRegExp measure section variable parameter. This can be used to properly escape with \ all PCRE Regular Expression reserved characters, which are . ^ $ * + ? ( ) [ { \ |, in the string value of the measure.

This can be useful with IfMatchActions, if you want to for instance see if two measure have the same string value, but can't be sure that the value doesn't contain regular expression reserved characters.

Code: Select all

[MeasureOne]
Measure=String
String=[Some+Text]

[MeasureTwo]
Measure=String
String=[Some+Text]
IfMatch=[MeasureOne:EscapeRegExp]
IfMatchAction=[!Log "They are the same"]
IfNotMatchAction=[!Log "They are different"]
DynamicVariables=1
A simplistic example, but as you can see both [MeasureOne] and [MeasureTwo] contain the same text. When we want to compare them with IfMatch, we need to be sure that the "[" and "+" reserved characters are "escaped" when we use them in a regular expression match in IfMatch=[MeasureOne:EscapeRegExp]. The :EscapeRegExp section variable parameter will ensure this is done for you.

What this will actually do under the covers is:

IfMatch=\[Some\+Text]

In the example above, the "[" and "]" in the regular expression would create a "character class", and the "+" would be seen as a directive of "1 or more instances" instead of being used as literals, and would produce undesirable results.