It is currently March 28th, 2024, 11:07 am

Small RegExp Substitute quandry

Get help with creating, editing & fixing problems with skins
Post Reply
User avatar
Mor3bane
Posts: 943
Joined: May 7th, 2016, 7:32 am
Contact:

Small RegExp Substitute quandry

Post 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.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Small RegExp Substitute quandry

Post 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
User avatar
Mor3bane
Posts: 943
Joined: May 7th, 2016, 7:32 am
Contact:

Re: Small RegExp Substitute quandry

Post 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:
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Small RegExp Substitute quandry

Post by balala »

It happens sometimes.
Glad to help.
User avatar
balala
Rainmeter Sage
Posts: 16109
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Small RegExp Substitute quandry

Post 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.
User avatar
Mor3bane
Posts: 943
Joined: May 7th, 2016, 7:32 am
Contact:

Re: Small RegExp Substitute quandry

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

Re: Small RegExp Substitute quandry

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