It is currently April 26th, 2024, 8:19 pm

[Solved]RegexpSubstitut help

Get help with creating, editing & fixing problems with skins
misstake
Posts: 5
Joined: August 19th, 2021, 3:57 pm

[Solved]RegexpSubstitut help

Post by misstake »

Hi,
I got stuck on this part of my skin.

Code: Select all

[MeasureTail]
Measure=Script
ScriptFile=#@#scripts\TailFile.lua
UpdateDivider=#SecondsBetweenRead#

[MeasureLine1]
Measure=String
RegExpSubstitut=1
Substitute="^(\d{1,2})\/(\d{1,2})\/(\d{4})\s(\d{2}):(\d{2}):(\d{2})\sAM,(\d{1,4}RPM)$":""
Honestly, I just don't understand how RegExp works... :Whistle
I tried different things... but it doesn't even change a single character.

Here is the string I'm trying to substitute to only keep the value (I don't even want the postfix):

Code: Select all

23/8/2021 23:17:24 PM,1494RPM
It's from iCue CSV log file.

I also wonder if it would be possible to directly get the value from the file instead of the whole string but it's starting to be a bit too complicated for me. I'm currently using Script that I found on this forum to get it :

Code: Select all

function Initialize()
      
	fileToRead = SKIN:GetVariable('FileToRead')
	linesToTail = tonumber(SKIN:GetVariable('LinesToTail'))
	childPrefix = SKIN:GetVariable('ChildPrefix')

end

function Update()
	local inputFile = io.open(fileToRead, 'r')
	local text, ch
	local pos = -1
	local i = 1

	repeat
		inputFile:seek("end", pos - 1)
		ch = inputFile:read(1)
		if ch == '\n' then
			text = inputFile:read(-pos)
			if text ~= nil then
				SKIN:Bang('!SetOption', childPrefix..i, 'String', string.match(text, "^(.-)\n"))
				SKIN:Bang('!UpdateMeasure', childPrefix..i)
				i = i + 1
				pos = pos - 1
			end
		end
		pos = pos - 1
	until (i > linesToTail)

	io.close(inputFile)

	SKIN:Bang('!UpdateMeter', '*')
	SKIN:Bang('!Redraw')

	return 0
end
Thank you
Last edited by misstake on August 25th, 2021, 10:53 pm, edited 1 time in total.
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: RegexpSubstitut help

Post by death.crafter »

misstake wrote: August 24th, 2021, 4:40 pm Hi,
I got stuck on this part of my skin.

Code: Select all

[MeasureTail]
Measure=Script
ScriptFile=#@#scripts\TailFile.lua
UpdateDivider=#SecondsBetweenRead#

[MeasureLine1]
Measure=String
RegExpSubstitut=1
Substitute="^(\d{1,2})\/(\d{1,2})\/(\d{4})\s(\d{2}):(\d{2}):(\d{2})\sAM,(\d{1,4}RPM)$":""
You have a typo:

RegExpSubstitute=1

Which part do you exactly want and which part you don't?

23/8/2021 23:17:24 PM,1494RPM

For the RPM part

Code: Select all

Substitute = "^.*,(\d{1,4})RPM$" : "\1"
Last edited by death.crafter on August 24th, 2021, 9:12 pm, edited 1 time in total.
from the Realm of Death
misstake
Posts: 5
Joined: August 19th, 2021, 3:57 pm

Re: RegexpSubstitut help

Post by misstake »

Well... You're right. I forgot an E... I feel so stupid...

Your code return 1494
It removed date time and postfix exactly like I wanted.
Thanks a lot.
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: RegexpSubstitut help

Post by death.crafter »

misstake wrote: August 24th, 2021, 8:39 pm Well... You're right. I forgot an E... I feel so stupid...

Your code return 1494
It removed date time and postfix exactly like I wanted.
Thanks a lot.
The regex was faulty sorry.

Use this one:

Code: Select all

Substitute = "^.*,(\d*)RPM$" : "\1"
from the Realm of Death
misstake
Posts: 5
Joined: August 19th, 2021, 3:57 pm

Re: RegexpSubstitut help

Post by misstake »

Hi,

Your code works great.
I installed new sensors so I had to change it a bit in order to work. I don't know if it's correct but it seems to works.
Thank you.

24/8/2021 23:08:17 PM,37.88°C,1500RPM,2115RPM

Code: Select all

[MeasureTail]
Measure=Script
ScriptFile=#@#scripts\TailFile.lua
UpdateDivider=#SecondsBetweenRead#

[MeasureLine1]
Measure=String

[measureCoolantTemp]
Measure=String
String=[measureline1]
RegExpSubstitute=1
Substitute="^.*,(.*),(\d*)RPM,(\d*)RPM$":"\1"
DynamicVariables=1

[measureCPUFan2]
Measure=String
String=[measureline1]
RegExpSubstitute=1
Substitute="^.*,(.*),(\d*)RPM,(\d*)RPM$":"\3"
DynamicVariables=1

[measureSysFan2]
Measure=String
String=[measureline1]
RegExpSubstitute=1
Substitute="^.*,(.*),(\d*)RPM,(\d*)RPM$":"\2"
DynamicVariables=1
User avatar
Yincognito
Rainmeter Sage
Posts: 7175
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: RegexpSubstitut help

Post by Yincognito »

death.crafter wrote: August 24th, 2021, 9:13 pm The regex was faulty sorry.

Use this one:

Code: Select all

Substitute = "^.*,(\d*)RPM$" : "\1"
You're becoming an expert on regex, that's nice! ;-)
As a side note, to prevent getting the literal \1 if by any chance the capture is the empty string (annoying behavior, I know), you could just delete everything from the start up to comma+space, along with the RPM+end part, in a (?:|) OR non-capture group.
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: RegexpSubstitut help

Post by death.crafter »

Yincognito wrote: August 25th, 2021, 10:22 am You're becoming an expert on regex, that's nice! ;-)
As a side note, to prevent getting the literal \1 if by any chance the capture is the empty string (annoying behavior, I know), you could just delete everything from the start up to comma+space, along with the RPM+end part, in a (?:|) OR non-capture group.
Didn't get you... Can you give an example? See my regexes are built up of . and *
from the Realm of Death
User avatar
Yincognito
Rainmeter Sage
Posts: 7175
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: RegexpSubstitut help

Post by Yincognito »

death.crafter wrote: August 25th, 2021, 1:35 pm Didn't get you... Can you give an example? See my regexes are built up of . and *
Yep, sorry, the OP only later showed that there are 2 RPM values instead of 1 or that there are multiple commas in that string, plus the probability to have no number (aka the empty string) in front of the RPM parts is rather low, but it's doable either way...

Test string: 24/8/2021 23:08:17 PM,37.88°C,1500RPM,2115RPM
1st RPM: Substitute="(?:^(?:.*?,){2}|RPM.*$)":""
2nd RPM: Substitute="(?:^(?:.*,){1}|RPM.*$)":""

The ? after the * means ungreedy matching in this case (well, actually toggling the greedy flag, in general), aka the equivalent of the (?U) flag, to match as less chars as possible. Other than that, it's just . and * as you said.

Of course, this could have been done using lookaheads as well, like:
1st RPM: Substitute="(?:^.*?,(?=\d*RPM)|RPM.*$)":""
2nd RPM: Substitute="(?:^.*,(?=\d*RPM)|RPM.*$)":""
but this is more than just . and *, so even if it's simpler in this case than the above (a single char difference between the 1st and 2nd RPM), I posted the latter first.

Both variants avoid capturing, instead deleting the unwanted parts. For cases where an empty capture (which is returned as the literal \1 and such by Rainmeter) is a possibility, it's a good way to avoid such unwanted "surprises".
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth
User avatar
death.crafter
Rainmeter Sage
Posts: 1399
Joined: April 24th, 2021, 8:13 pm

Re: RegexpSubstitut help

Post by death.crafter »

Yincognito wrote: August 25th, 2021, 4:17 pm Yep, sorry, the OP only later showed that there are 2 RPM values instead of 1 or that there are multiple commas in that string, plus the probability to have no number (aka the empty string) in front of the RPM parts is rather low, but it's doable either way...

Test string: 24/8/2021 23:08:17 PM,37.88°C,1500RPM,2115RPM
1st RPM: Substitute="(?:^(?:.*?,){2}|RPM.*$)":""
2nd RPM: Substitute="(?:^(?:.*,){1}|RPM.*$)":""

The ? after the * means ungreedy matching in this case (well, actually toggling the greedy flag, in general), aka the equivalent of the (?U) flag, to match as less chars as possible. Other than that, it's just . and * as you said.

Of course, this could have been done using lookaheads as well, like:
1st RPM: Substitute="(?:^.*?,(?=\d*RPM)|RPM.*$)":""
2nd RPM: Substitute="(?:^.*,(?=\d*RPM)|RPM.*$)":""
but this is more than just . and *, so even if it's simpler in this case than the above (a single char difference between the 1st and 2nd RPM), I posted the latter first.

Both variants avoid capturing, instead deleting the unwanted parts. For cases where an empty capture (which is returned as the literal \1 and such by Rainmeter) is a possibility, it's a good way to avoid such unwanted "surprises".
Ohh that's what you meant by empty string. Yup I got the point (but not the regex :rofl: ). I have to learn more than ., +and *s lmao.
from the Realm of Death
User avatar
Yincognito
Rainmeter Sage
Posts: 7175
Joined: February 27th, 2015, 2:38 pm
Location: Terra Yincognita

Re: RegexpSubstitut help

Post by Yincognito »

death.crafter wrote: August 25th, 2021, 4:24 pm Ohh that's what you meant by empty string. Yup I got the point (but not the regex :rofl: ). I have to learn more than ., +and *s lmao.
Eh, don't worry, we've all started from . and * with regex - and most of the times that's enough. ;-)

A whole / longer pattern in regex might seem complicated to "decipher" in its final form, but in essence they're just a quite limited number of single characters (or character classes) one after the other, no big deal. The entire regex is roughly just:

- 23 character classes (with about 13 of them widely used)
- 8 anchors (with about 4 of them widely used)
- 13 escaped character groups (with about 3 of them widely used, one being the reserved characters group)
- 10 groups and references (with about 3 of them widely used)
- 5 lookarounds (with about 2 to 4 of them widely used)
- 7 quantifiers and alternation (with about 3 or 4 of them widely used)
- 5 "specials" (with about 1 of them widely used)
- 3 substitution (with about 1 of them widely used)
- 6 flags (with about 3 of them widely used)

So, about 36 chars to combine and remember what they mean, for regular / medium use - slightly more than the English alphabet. The rest is just the skill of the user to manipulate these, that's all. 8-)

That being said, math is also just a couple of digits, symbols, functions and operations, yet it can become difficult as well, so... :confused:
Profiles: Rainmeter ProfileDeviantArt ProfileSuites: MYiniMeterSkins: Earth