New IfMatchActions

Changes made during the Rainmeter 3.1 beta cycle.
User avatar

New IfMatchActions

January 5th, 2014, 4:37 pm
jsmorley
Developer   [15579 posts]

We have added new IfMatchAction options. These can be used on any measure, allowing you compare the string value of the measure with a PCRE Regular Expression. The comparison in IfMatch will be "true" if the regular expression succeeds, and "false" if it does not.

Actions can then be taken based on IfMatchAction (true) or IfNotMatchAction (false).

There can be as many IfMatch / IfMatchAction / IfNotMatchAction sets as you need, by appending a number. IfMatch2... / IfMatchAction2... / IfNotMatchAction2...

As with IfActions, the action is only fired once when the result becomes "true" or "false". An additional IfMatchMode option can be set to "1" to have the actions fire on each update of the measure.

Examples:

Code: Select all

[MeasureMyString]
Measure=String
String=Hello World
IfMatch=.*World.*
IfMatchAction=[!Log "World was found"]
IfNotMatchAction=[!Log "World was not found"]


Code: Select all

[MeasureWeatherConditions]
Measure=Plugin
Plugin=WebParser
URL=[MeasureWeatherParent]
StringIndex=5
IfMatch=(?i)sunny|fair
IfMatchAction=[!SetOption MeterConditions FontColor #Yellow#]
IfNotMatchAction=[!SetOption MeterConditions FontColor #Grey#]


Logical OR and AND testing can be done in the regular expression, using the | character to support OR and Lookahead Assertions (?=..) to support AND.

Note that any regular expression reserved characters, which are . ^ $ * + ? ( ) [ { \ |, must be escaped with \ when used as a literal in IfMatch.

Other IfMatch Eamples:

String=Hello World

IfMatch=(?=.*Hello)(?=.*World) AND order independent
IfMatch=Hello.*World AND order dependent

IfMatch=Hello|World OR

String=Red Green Blue

IfMatch=(?=.*Red)(?=.*Green)|Blue "(1 AND 2) order independent OR 3"

String=Red Green Blue Orange

IfMatch=(?=.*Red)(?=.*Green)|(?=.*Blue)(?=.*Orange) "(1 AND 2) order independent OR (3 AND 4) order independent"

IfMatch=Red Green|Blue Orange "(1 AND 2) order and position dependent OR (3 AND 4) order and position dependent"

String=net internet

IfMatch=\bnet\b Use Word Boundary to match on net but not on internet

String=there was an error

IfMatch=error IfMatchAction will fire as it is "true"

String=everything went fine

IfMatch=error IfNotMatchAction will fire as it is "false"

More help:

Just tons of manuals, tutorials, guides, tools and other help for regular expressions can be found at Regular Expression Options in Rainmeter.
User avatar

Re: New IfMatchActions

January 5th, 2014, 7:08 pm
killall-q
   [284 posts]

Hey, just what I needed! One less lua function to keep around.

What's the difference between "IfMatch=x" and "IfMatch=(?=x)"? Also, how to match an empty string?

Edit:

Code: Select all

IfMatch=.
IfNotMatchAction=!BlahBlah...


Nice that the new version doesn't bug me to move my Rainmeter.ini to %USERDATA%.
User avatar

Re: New IfMatchActions

January 5th, 2014, 8:17 pm
jsmorley
Developer   [15579 posts]

Code: Select all

[Rainmeter]
Update=1000
AccurateText=1
DynamicWindowSize=1

[MeasureCPUVendor]
Measure=Registry
RegHKey=HKEY_LOCAL_MACHINE
RegKey=HARDWARE\DESCRIPTION\System\CentralProcessor\0
RegValue=VendorIdentifier
UpdateDivider=-1
IfMatch=(?i).*amd.*
IfMatchAction=[!SetOption MeterCPUVendor ImageName "AMD_CPU.png"]
IfMatch2=(?i).*intel.*
IfMatchAction2=[!SetOption MeterCPUVendor ImageName "Intel_CPU.png"]

[MeterCPUVendor]
Meter=Image
ImagePath=#@#Images\


Intel_CPU.png

AMD_CPU.png
User avatar

Re: New IfMatchActions

January 5th, 2014, 8:20 pm
jsmorley
Developer   [15579 posts]

killall-q wrote:Hey, just what I needed! One less lua function to keep around.

What's the difference between "IfMatch=x" and "IfMatch=(?=x)"? Also, how to match an empty string?

Edit:

Code: Select all

IfMatch=.
IfNotMatchAction=!BlahBlah...



(?=...) is a Lookahead Assertion. It's worth looking at the various regular expression tutorials and other help pages we have linked to all over the place. Generally, you won't use a lookahead assertion to find text in a string, it really isn't needed in this context.

String=the name is jsmorley it is
IfMatch=jsmorley : true
IfMatch=.*jsmorley.* : true
IfMatch=(?=jsmorley) : true
IfMatch=(?=.*jsmorley.*) : true

However it can be useful for creating an AND condition, which is position / order independent.

String=One Two Three Four
IfMatch=(?=.*One)(?=.*Four) : true
IfMatch=(?=.*Four)(?=.*One) : true
IfMatch=(?=.*One)(?=.*Five) : false

An AND that is order dependent is easy

String=One Two Three Four
IfMatch=.*One.*Four : true
IfMatch=.*Four.*One : false

Finding an empty string is:

IfMatch=^$
IfMatchAction=[Play "#CURRENTPATH#Crickets.wav"]
User avatar

Re: New IfMatchActions

January 5th, 2014, 9:57 pm
moshi
   [1852 posts]

this is the one i like best. i think i will use this with feed readers to "scan" the content for certain "tags".
User avatar

Re: New IfMatchActions

January 6th, 2014, 10:31 pm
jsmorley
Developer   [15579 posts]

Although with the ability to have as many IfMatchN options as you want on a measure, there may seldom be a real need, you can also do "negative" checking...

String=There was an error at some point
IfMatch=^(?!.*error).*
IfMatchAction=[!SetOption MeterOne Text "Good, no 'error' found"]
IfNotMatchAction=[!SetOption MeterOne Text "Hey, 'error' was found"]

You can also do "negative" checking with AND

String=One Two Three Four
IfMatch=^(?=.*One)(?=.*Two)(?=.*Three)(?!.*Four)
IfMatchAction=[!SetOption MeterOne Text "Good, no 'Four' found"]
IfNotMatchAction=[!SetOption MeterOne Text "Hey, 'Four' was found"]

Lookarounds (particularly negative lookarounds) are a tad complicated. If you have trouble getting them to work the way you want, don't feel bad. I pretty much just throw things at the wall until something sticks myself...

Return to “Rainmeter 3.1”



Who is online

Users browsing this forum: No registered users and 1 guest