It is currently March 28th, 2024, 5:10 pm

New IfMatchActions

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

New IfMatchActions

Post by jsmorley »

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
killall-q
Posts: 305
Joined: August 14th, 2009, 8:04 am

Re: New IfMatchActions

Post by killall-q »

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
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: New IfMatchActions

Post by jsmorley »

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
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: New IfMatchActions

Post by jsmorley »

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
moshi
Posts: 1740
Joined: November 13th, 2012, 9:53 pm

Re: New IfMatchActions

Post by moshi »

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
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: New IfMatchActions

Post by jsmorley »

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...
doomerino
Posts: 10
Joined: May 9th, 2014, 8:42 pm

Re: New IfMatchActions

Post by doomerino »

Hey,
i have the Problem with my Webparser to get matches of a Dynamic List. Sometimes the list has 1 User, sometimes 3 User or more...
how can i solve the "regex -1 NOMATCH error", if the list has only 1 item?

everytime the userlist has less user than Regex-Strings, i get a -1 Nomatch error..

i tried:

Code: Select all


[Variables]
RegExp1="(?siU).*<u>Userliste</u><br>(.*)<br>"
RegExp2="(?siU).*<u>Userliste</u><br>(.*)<br>(?(?=.*)(.*))<br>"
RegExp3="(?siU).*<u>Userliste</u><br>(.*)<br>(?(?=.*)(.*))<br>(?(?=.*)(.*))<br>"
RegExp4="(?siU).*<u>Userliste</u><br>(.*)<br>(?(?=.*)(.*))<br>(?(?=.*)(.*))<br>(?(?=.*)(.*))<br>.*"
Status=false   ;   to check if the condition works


[MeasureUserListe]
Measure=Plugin
Plugin=WebParser.dll
UpdateDivider=5
LogSubstringErrors=0
StringIndex=1
DynamicVariables=1
Url=#URL#
RegExp=#RegExp3#
IfMatch=""
IfMatchAction=[!SetOption MeasureUserListe RegExp=#RegExp1#][!SetOption Variables Status=true][!CommandMeasure MeasureUserListe "Update"]
what is the correct "ifmatch"-condition for an EMPTY Regex-Error?
Or is there a easier Way to handle "No Match" Regex-Strings?

Thanks for any Hints, Stay healthy
Alex
User avatar
jsmorley
Developer
Posts: 22628
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: New IfMatchActions

Post by jsmorley »

To check if a string is "empty" using regular expression, you would use:

IfMatch=^$

That means "If the string starts ^ and immediately ends $.

P.S. to Srikrishna...

ANY IfMatch or IfCondition that includes a !Refresh bang in the resulting action is an absolutely certain, fatal, endless loop that will crash Rainmeter. If something is "true" or "false" now, it will still be "true" or "false" when the skin refreshes and the test is executed again. This will happen over and over as fast as Rainmeter can refresh, which is really, really fast. Death will be within seconds.
doomerino
Posts: 10
Joined: May 9th, 2014, 8:42 pm

Re: New IfMatchActions

Post by doomerino »

jsmorley wrote: April 1st, 2020, 8:10 pm To check if a string is "empty" using regular expression, you would use:

IfMatch=^$

That means "If the string starts ^ and immediately ends $.

Thank for Your Help,

but that dont work for me.. i have a list at the moment with 1 User.... and start with the #RegExp3# for 3 Strings...
so i have 1 hit and 2 Empty Regex-Strings.. the if condition dont trigger and the "Status=false" variable didnt change, too..

is there a logic Fault?! :?

thanks Jsmorley
doomerino
Posts: 10
Joined: May 9th, 2014, 8:42 pm

Re: New IfMatchActions

Post by doomerino »

Got it to Work...
Thanks for the Tip!

Greetings
Post Reply