It is currently May 23rd, 2022, 4:27 am

New :EscapeRegExp Section Variable parameter

Changes made during the Rainmeter 3.1 beta cycle.
User avatar
Posts: 22519
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


IfMatchAction=[!Log "They are the same"]
IfNotMatchAction=[!Log "They are different"]
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:


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.