It is currently April 3rd, 2020, 11:26 am

RegExpSubstitute trouble (again)

Help with creating, editing & fixing problems with skins
User avatar
StArL0rd84
Posts: 341
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

RegExpSubstitute trouble (again)

Post by StArL0rd84 »

Hi everyone, back again to mess with rainmeter after a long break.
I hope you and yours are doing ok in these trying times. :welcome:

Making a skin to control my 3D printer over WiFi via a ESP2866 module.
I am using WebParser to send the requests and receive the string value.
And i am able to do things like movement, extrude plastic, change color of Neopixel RGB strips and set temperatures.

But when i poll for temperatures they arrive as a single string "value".
Current nozzle temp, target nozzle temp, Current bed temp, target bed temp.
Return string: ok T:20.16 /0.00 B:20.62 /0.00 @:0 B@:

I then need to seperate the temperatures so i can format the text as i need for the skin.
Had some luck using RegExpSubstitute to format the text.
But after several days of research and poking at it, i must give in and ask for guidance.
Used this cheat sheet: https://www.debuggex.com/cheatsheet/regex/pcre

Code: Select all

[mTemp]
Measure=WebParser
URL=http://#ESPIP#/command?commandText=M105
RegExp=(?siU)ok T:(.*) B@:
UpdateRate=5
Substitute="":"Printer Offline"

[mTempSlit1]
Measure=String
String=ok T:20.16 /0.00 B:20.62 /0.00 @:0 B@:
;[mTemp]
RegExpSubstitute=1
Substitute="^(.{1,}).\d\d\s(.{1,})+$":"\1"
To start with, i tried to remove everything after each .\d\d\s but it only had an effect on the last temp...
sub2.png
You do not have the required permissions to view the files attached to this post.
Last edited by StArL0rd84 on March 20th, 2020, 7:29 pm, edited 1 time in total.
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 20433
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RegExpSubstitute trouble (again)

Post by jsmorley »

Something like this maybe:

Code: Select all

[MeasureTemp1]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubsitute=1
Substitute=(?siU)T:(.*)\..*/.\..*B:.*\..*/.*\.

[MeasureTemp2]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubsitute=1
Substitute=(?siU)T:.*\..*/(.*)\..*B:.*\..*/.*\.

[MeasureTemp3]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubsitute=1
Substitute=(?siU)T:.*\..*/.*\..*B:(.*)\..*/.*\.

[MeasureTemp4]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubsitute=1
Substitute=(?siU)T:.*\..*/.*\..*B:.*\..*/(.*)\.

1.jpg
You do not have the required permissions to view the files attached to this post.
User avatar
StArL0rd84
Posts: 341
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: RegExpSubstitute trouble (again)

Post by StArL0rd84 »

Hmm didn't work, but i missed that tool in my research.
will give it a try
sub3.png
You do not have the required permissions to view the files attached to this post.
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 20433
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RegExpSubstitute trouble (again)

Post by jsmorley »

All I can go by is the assumption that the string will follow a pattern of:

ok T:20.16 /0.00 B:20.62 /0.00 @:0 B@:

And that you want the integer portion of the four possible temperatures.

If you use the RegExp I provided, it should return them.

https://docs.rainmeter.net/tips/webparser-debugging-regexp/#RainRegExp
User avatar
jsmorley
Developer
Posts: 20433
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RegExpSubstitute trouble (again)

Post by jsmorley »

All four, in RainRegExp:

(?siU)T:(.*)\..*/(.*)\..*B:(.*)\..*/(.*)\.

Just the first one:

(?siU)T:(.*)\..*/.*\..*B:.*\..*/.*\.

So basically just move the (capture) around to get the different values.
User avatar
jsmorley
Developer
Posts: 20433
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RegExpSubstitute trouble (again)

Post by jsmorley »

This is really pretty straightforward, it's more or less just skip .* and capture (.*). The only gotcha here is that the . character, which is used as the decimal point character in the string, is also a reserved regular expression character meaning "any character". So it use it as a literal, we need to escape \ it.

So for instance in:

\..*

We are saying look for a literal \ dot ., followed by any character . zero or more times *.
User avatar
StArL0rd84
Posts: 341
Joined: February 8th, 2015, 10:07 pm
Location: EU, Denmark.

Re: RegExpSubstitute trouble (again)

Post by StArL0rd84 »

Can get it work in the tool, but not in the skin.
sub4.png
You do not have the required permissions to view the files attached to this post.
(#WorkTime# = 1 ? #Work# : ([mEnergyLoss:%] > 100 ? #SleepMode# : #Rainmeter#))
User avatar
jsmorley
Developer
Posts: 20433
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RegExpSubstitute trouble (again)

Post by jsmorley »

Try this:

Code: Select all

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

[Variables]

[MeasureTempAll]
Measure=String
String=ok T:20.16 /0.00 B:20.62 /0.00 @:0 B@:

[MeasureTemp1]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:([\d]+)\..*":"\1"

[MeasureTemp2]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:.*\..*/([\d]+)\..*":"\1"

[MeasureTemp3]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:.*\..*/.*\..*B:([\d]+)\..*":"\1"

[MeasureTemp4]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:.*\..*/.*\..*B:.*\..*/([\d]+)\..*":"\1"
User avatar
jsmorley
Developer
Posts: 20433
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RegExpSubstitute trouble (again)

Post by jsmorley »

Another way to approach this, that perhaps doesn't require as much typo-risky "tweaking" of each Substitute, is:

Code: Select all

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

[Variables]

[MeasureTempAll]
Measure=String
String=ok T:20.16 /0.00 B:20.62 /0.00 @:0 B@:

[MeasureTemp1]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:([\d]+)\..*/([\d]+)\..*B:([\d]+)\..*/([\d]+)\..*":"\1"

[MeasureTemp2]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:([\d]+)\..*/([\d]+)\..*B:([\d]+)\..*/([\d]+)\..*":"\2"

[MeasureTemp3]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:([\d]+)\..*/([\d]+)\..*B:([\d]+)\..*/([\d]+)\..*":"\3"

[MeasureTemp4]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:([\d]+)\..*/([\d]+)\..*B:([\d]+)\..*/([\d]+)\..*":"\4"
That way you can just copy and paste the entire Substitute, then just change the "index" number you want to use.

This will also work:

Code: Select all

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

[Variables]

[MeasureTempAll]
Measure=String
String=ok T:20.16 /0.00 B:20.62 /0.00 @:0 B@:

[MeasureTemp1]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:(.*)\..*/(.*)\..*B:(.*)\..*/(.*)\..*":"\1"

[MeasureTemp2]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:(.*)\..*/(.*)\..*B:(.*)\..*/(.*)\..*":"\2"

[MeasureTemp3]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:(.*)\..*/(.*)\..*B:(.*)\..*/(.*)\..*":"\3"

[MeasureTemp4]
Measure=String
String=[MeasureTempAll]
DynamicVariables=1
RegExpSubstitute=1
Substitute=".*T:(.*)\..*/(.*)\..*B:(.*)\..*/(.*)\..*":"\4"

[MeterOne]
Meter=String
FontSize=11
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
User avatar
jsmorley
Developer
Posts: 20433
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: RegExpSubstitute trouble (again)

Post by jsmorley »

The difference between WebParser (and RainRegExp) and RegExpSubstitute is this:

With a regular expression Substitute, there are two conditions which should be met.

First, you should have the "Search for" bit of Substitute="Search for":"Replace with" match the ENTIRE string value of the measure. It generally shouldn't just match part of the string, like you almost always do with WebParser. Even if you are only (capturing) a tiny bit of the string value, you should have an expression that matches the entire string.

This is because the intent of Substitute is to REPLACE, not strictly to FIND. It will replace anything it finds based on the expression, but will just leave alone anything the match doesn't find. So to get rid of stuff, you HAVE to "find" it, so it can be "replaced" with nothing...

The intent of WebParser is to "find" bits of information and return them. The intent of Substitute is to "replace" JUST the bits of information that the expression finds. Anything it doesn't find will just remain there, same as it was...

That is why I put .*, to skip any number of any character, at the beginning and end of the "Search for" bit.

Second, you need to use (capture) to extract just the bits you want. With this, it is much like WebParser, in that each (capture) will create an "index" number that can be used to retrieve the actual value of any given (capture).

For that you use \1, \2, \3 ... in the "Replace with" bit of the Substitute.