Page 1 of 1

Small RegExp Substitute quandry

Posted: January 5th, 2017, 12:57 pm
by Mor3bane
Hi, I have a Internet Connectivity skin, and I have half of the substitute working correctly.

The PlugIn result is a "1" for connected, and a -1 for Off Line

Here is the relevant mater:

Code: Select all

[MeasureConnectivity]
Measure=Plugin
Plugin=SysInfo
SysInfoType=INTERNET_CONNECTIVITY
Substitute="1":"Connected","^-1$":"Off Line"
The way it is coded now gives the correct "connected" result, however the "offline" result is "-Connected".

Notice the minus being transposed onto the first substituteas the "offline" indicator.

I have tried several variations with no positive result for the expected "Off Line" substitution.

What am I missing? Any suggestions are appreciated.

Re: Small RegExp Substitute quandry

Posted: January 5th, 2017, 1:17 pm
by balala
Mor3bane wrote:The way it is coded now gives the correct "connected" result, however the "offline" result is "-Connected".
Because according to the first part of the Substitute option, 1 is always substituted by Connected, even if it has a minus before.
To fix it, add to the [MeasureConnectivity] measure a RegExpSubstitute=1 option and add the ^ and $ around the 1, in the Substitute:

Code: Select all

[MeasureConnectivity]
Measure=Plugin
Plugin=SysInfo
SysInfoType=INTERNET_CONNECTIVITY
RegExpSubstitute=1
Substitute="^1$":"Connected","^-1$":"Off Line"
^ represents the beginning of the expression, while $ represents the end of it. So, the "^1$":"Connected" will be applied only if the entire expression is 1 (no more, no less), and "^-1$":"Off Line" will be applied when the whole expression is -1. Usually the Substitute option don't use these regular expressions, to do that, you need the RegExpSubstitute=1 option: https://docs.rainmeter.net/manual/measures/general-options/substitute/#RegExpSubstitute

Re: Small RegExp Substitute quandry

Posted: January 5th, 2017, 9:39 pm
by Mor3bane
Thanks balala

I even tried that, but I was still missing RegExpSubstitute=1

I knew that was needed too, just couldn't see past the issue.

Cheers! :thumbup:

Re: Small RegExp Substitute quandry

Posted: January 5th, 2017, 9:45 pm
by balala
It happens sometimes.
Glad to help.

Re: Small RegExp Substitute quandry

Posted: January 6th, 2017, 8:13 am
by balala
One more: what you wanted can be done even without using the regular expressions, just have to be careful to the order of substitutions. Make sure that when the first substitution is made, the second won't be:

Code: Select all

[MeasureConnectivity]
Measure=Plugin
Plugin=SysInfo
SysInfoType=INTERNET_CONNECTIVITY
Substitute="-1":"Off Line","1":"Connected"
In this case, if the value of the measure is -1, it'll be substituted by Off Line and because Off Line don't contain the character 1, the second substitution is not applied, so you'll get Off Line. If on the other hand, the value returned by the measure is 1, the first substitution is ignored, because the value of the measure don't contain -1. In this case, the second substitution will be applied, so the measure will return Connected.
In this case you don't need the RegExpSubstitute=1 option.

Re: Small RegExp Substitute quandry

Posted: January 9th, 2017, 3:53 pm
by Mor3bane
balala wrote:One more: what you wanted can be done even without using the regular expressions, just have to be careful to the order of substitutions. Make sure that when the first substitution is made, the second won't be:

Code: Select all

[MeasureConnectivity]
Measure=Plugin
Plugin=SysInfo
SysInfoType=INTERNET_CONNECTIVITY
Substitute="-1":"Off Line","1":"Connected"
In this case, if the value of the measure is -1, it'll be substituted by Off Line and because Off Line don't contain the character 1, the second substitution is not applied, so you'll get Off Line. If on the other hand, the value returned by the measure is 1, the first substitution is ignored, because the value of the measure don't contain -1. In this case, the second substitution will be applied, so the measure will return Connected.
In this case you don't need the RegExpSubstitute=1 option.
That's cool! It works on the "fall through" principle which is very common in higher language programming. Given that I know that just means I get experience another "why didn't I remember that" moment :D

Re: Small RegExp Substitute quandry

Posted: January 9th, 2017, 3:57 pm
by jsmorley
The key is to remember that Substitute works on each comma-separated "segment" of the Substitute option in order, and with the action of later segments operating on the results of earlier...