It is currently October 15th, 2024, 8:42 am

New :EscapeRegExp Section Variable parameter

Changes made during the Rainmeter 3.1 beta cycle.
User avatar
jsmorley
Developer
Posts: 22856
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

New :EscapeRegExp Section Variable parameter

Post 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.