It is currently April 24th, 2024, 11:36 am

Regex

Get help with creating, editing & fixing problems with skins
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Regex

Post by kyriakos876 »

Hello,
I have this string:

20,20,20|10,10,10|20,20,20

I want a regex that will match only if the middle ones are not 20s and the outer ones only 20s.
So, this wouldn't match:

20,20,20|20,20,20|20,20,20

and this wouldn't match either

10,10,10|10,10,10|20,20,20

Only this would match:

20,20,20|anything BUT 20,anything BUT 20,anything BUT 20|20,20,20

I hope you understand.

-Thanks in advance.
User avatar
ikarus1969
Posts: 571
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Regex

Post by ikarus1969 »

Just to be sure: the string will always consist of 2-digit-numbers? No space in between the numbers and the ,, |?

In that case try: ^(20,20,20\|[^2]\d,[^2]\d,[^2]\d\|20,20,20)$

Also try the Regular-Expression tester from jsmorley where you can play around with regular-expressions.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

ikarus1969 wrote: November 6th, 2018, 8:07 am Just to be sure: the string will always consist of 2-digit-numbers? No space in between the numbers and the ,, |?

In that case try: ^(20,20,20\|[^2]\d,[^2]\d,[^2]\d\|20,20,20)$

Also try the Regular-Expression tester from jsmorley where you can play around with regular-expressions.
The numbers will be colors. All which will be variables thus not standard, because obviously a color might have 2 digits 3 or even 1. But yeah, no spaces. It's like
A|not A|A
Where A= color code (ex. 255,255,255 or 1,1,1 or 30,1,167 without alpha)
User avatar
ikarus1969
Posts: 571
Joined: February 28th, 2011, 3:20 pm
Location: Vienna, Austria

Re: Regex

Post by ikarus1969 »

then i have to wait too for another user; i'm not good in "lookahead" / "lookbehind" assertions of regular expressions...
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

ikarus1969 wrote: November 6th, 2018, 10:49 am then i have to wait too for another user; i'm not good in "lookahead" / "lookbehind" assertions of regular expressions...
Well thanks for trying anyway :)
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5406
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Regex

Post by eclectic-tech »

I would capture each color set in a child measure and test if they match 20,20,20.
Then increment a #Match# variable if they match the first and last, but not the center color; this will result in a value of 3 when the criteria is met.

I created an ANSI text file to test named Color.txt in the same folder as the code below. It contains the string: 20,20,20|10,10,10|20,20,20

The code below will parse that file, separate the 3 color sets and increment the variable if they match the first and last values and do not match the center value.

Code: Select all

; Hello,
; I have this string:

; 20,20,20|10,10,10|20,20,20

; I want a regex that will match only if the middle ones are not 20s and the outer ones only 20s.
; So, this wouldn't match:

; 20,20,20|20,20,20|20,20,20

; and this wouldn't match either

; 10,10,10|10,10,10|20,20,20

; Only this would match:

; 20,20,20|anything BUT 20,anything BUT 20,anything BUT 20|20,20,20

; I hope you understand.

[Variables]
Match=0

[MeasureWebParser]
Measure=WebParser
URL="file://#CurrentPath#Color.txt"
RegExp=(?si)(\d{1,3},\d{1,3},\d{1,3})\|(\d{1,3},\d{1,3},\d{1,3})\|(\d{1,3},\d{1,3},\d{1,3})
FinishAction=[!EnableMeasureGroup Match][!UpdateMeasureGroup Match]

; Adds 1 to variable #Match# if string equals 20,20,20
[MeasureWebParser1]
Group=Match
Measure=WebParser
Url=[MeasureWebParser]
StringIndex=1
IfMatch=20,20,20
IfMatchAction=[!SetVariable Match (#Match#+1)]
IfNotMatchAction=[!SetVariable Match (#Match#-1)]
DynamicVariables=1
Disabled=1

; Adds 1 to variable #Match# if string does not equal 20,20,20
[MeasureWebParser2]
Group=Match
Measure=WebParser
Url=[MeasureWebParser]
StringIndex=2
IfMatch=20,20,20
IfMatchAction=[!SetVariable Match (#Match#-1)]
IfNotMatchAction=[!SetVariable Match (#Match#+1)]
DynamicVariables=1
Disabled=1

; Adds 1 to variable #Match# if string equals 20,20,20
[MeasureWebParser3]
Group=Match
Measure=WebParser
Url=[MeasureWebParser]
StringIndex=3
IfMatch=20,20,20
IfMatchAction=[!SetVariable Match (#Match#+1)]
IfNotMatchAction=[!SetVariable Match (#Match#-1)]
DynamicVariables=1
Disabled=1

; If the entire string is "20,20,20|{not20},{not20},{not20}|20,20,20" the value of #Match# will be 3
; Any other number means the strings do not match 
[MeasureMatch]
Measure=String
String=#Match#
IfCondition=(#Match#=3)
IfTrueAction=[!SetOption Meter Text "We have a COLOR MATCH!][!UpdateMeter *][!Redraw]
IfFalseAction=[!SetOption Meter Text "The colors DO NOT MATCH!"][!UpdateMeter *][!Redraw]
DynamicVariables=1

[Meter]
Meter=String
X=50
Y=50
FontSize=14
Text=""
DynamicVariables=1

[MeterColor]
Meter=String
MeasureName=MeasureWebParser1
MeasureName2=MeasureWebParser2
MeasureName3=MeasureWebParser3
X=r
Y=R
FontSize=14
Text=%1 | %2 | %3
You could do this with a variable containing your 3 colors and use RegExpSubstitute to achieve the same result, but I do not think you can do this with just 1 measure.

Hope this helps get you closer to an answer. :welcome:
User avatar
eclectic-tech
Rainmeter Sage
Posts: 5406
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Regex

Post by eclectic-tech »

Here is a solution using RegExpSubstitute on a variable... :Whistle

Code: Select all

; Hello,
; I have this string:

; 20,20,20|10,10,10|20,20,20

; I want a regex that will match only if the middle ones are not 20s and the outer ones only 20s.
; So, this wouldn't match:

; 20,20,20|20,20,20|20,20,20

; and this wouldn't match either

; 10,10,10|10,10,10|20,20,20

; Only this would match:

; 20,20,20|anything BUT 20,anything BUT 20,anything BUT 20|20,20,20

; I hope you understand.

[Variables]
MatchA=0
TestColor=20,20,20|10,10,10|20,20,20

; Solution using RegExpSubstitue
; Adds 1 to variable #MatchA# if string equals 20,20,20
[MeasureParser1a]
Measure=String
String=#TestColor#
RegExpSubstitute=1
Substitute="(.*)\|(.*)\|(.*)":"\1"
IfMatch=20,20,20
IfMatchAction=[!SetVariable MatchA (#MatchA#+1)]
IfNotMatchAction=[!SetVariable MatchA (#MatchA#-1)]
DynamicVariables=1

; Adds 1 to variable #MatchA# if string does not equal 20,20,20
[MeasureParser2a]
Measure=String
String=#TestColor#
RegExpSubstitute=1
Substitute="(.*)\|(.*)\|(.*)":"\2"
IfMatch=20,20,20
IfMatchAction=[!SetVariable MatchA (#MatchA#-1)]
IfNotMatchAction=[!SetVariable MatchA (#MatchA#+1)]
DynamicVariables=1

; Adds 1 to variable #MatchA# if string equals 20,20,20
[MeasureParser3a]
Measure=String
String=#TestColor#
RegExpSubstitute=1
Substitute="(.*)\|(.*)\|(.*)":"\3"
IfMatch=20,20,20
IfMatchAction=[!SetVariable MatchA (#MatchA#+1)]
IfNotMatchAction=[!SetVariable MatchA (#MatchA#-1)]
DynamicVariables=1

; If the entire string is "20,20,20|{not20},{not20},{not20}|20,20,20" the value of #MatchA# will be 3
; Any other number means the string is not a match
[MeasureMatcha]
Measure=String
String=#MatchA#
IfCondition=(#MatchA#=3)
IfTrueAction=[!SetOption MeterA Text "We have a COLOR MATCH!][!UpdateMeter *][!Redraw]
IfFalseAction=[!SetOption MeterA Text "The colors DO NOT MATCH!"][!UpdateMeter *][!Redraw]
DynamicVariables=1

[MeterA]
Meter=String
X=50
Y=10R
FontSize=14
Text=""
DynamicVariables=1

[MeterColorA]
Meter=String
MeasureName=MeasureParser1a
MeasureName2=MeasureParser2a
MeasureName3=MeasureParser3a
X=r
Y=R
FontSize=14
Text=%1 | %2 | %3

User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

I did this afternoon
Where check1,2,3 are the colors.

Code: Select all




[Controller]

Measure=String

String=#Check2#

IfMatch=^((?!#DefaultColorRGB#).)*$

IfMatchAction=[!SetVariable Passed "1"][!Update]

IfNotMatchAction=[!SetVariable Passed "0"][!Update]

DynamicVariables=1



[Controller2]

Measure=String

String=#Check1#,#Check3#

IfMatch=#DefaultColorRGB#,#DefaultColorRGB#

IfMatchAction=[!SetVariable Passed2 "1"][!Update]

IfNotMatchAction=[!SetVariable Passed2 "0"][!Update]

DynamicVariables=1



[Main]

Measure=Calc

Formula=1

IfCondition=(#Passed2# + #Passed# = 2)

IfTrueAction=[!SetOption MeterStatus Text "Taskbar is showing"][!Update]

IfFalseAction=[!SetOption MeterStatus Text "Taskbar is hidden"][!Update]

DynamicVariables=1


User avatar
eclectic-tech
Rainmeter Sage
Posts: 5406
Joined: April 12th, 2012, 9:40 pm
Location: Cedar Point, Ohio, USA

Re: Regex

Post by eclectic-tech »

kyriakos876 wrote: November 7th, 2018, 1:16 pm I did this afternoon
Where check1,2,3 are the colors.

Code: Select all




[Controller]

Measure=String

String=#Check2#

IfMatch=^((?!#DefaultColorRGB#).)*$

IfMatchAction=[!SetVariable Passed "1"][!Update]

IfNotMatchAction=[!SetVariable Passed "0"][!Update]

DynamicVariables=1



[Controller2]

Measure=String

String=#Check1#,#Check3#

IfMatch=#DefaultColorRGB#,#DefaultColorRGB#

IfMatchAction=[!SetVariable Passed2 "1"][!Update]

IfNotMatchAction=[!SetVariable Passed2 "0"][!Update]

DynamicVariables=1



[Main]

Measure=Calc

Formula=1

IfCondition=(#Passed2# + #Passed# = 2)

IfTrueAction=[!SetOption MeterStatus Text "Taskbar is showing"][!Update]

IfFalseAction=[!SetOption MeterStatus Text "Taskbar is hidden"][!Update]

DynamicVariables=1


The same basic idea using IfMatch to test string variables versus testing extracted RegExpSubstitute strings... :)

There are many paths to reach the same goal and we all choose how we get there... 8-)

Glad to hear you found your way :thumbup:
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

eclectic-tech wrote: November 7th, 2018, 1:33 pm The same basic idea using IfMatch to test string variables versus testing extracted RegExpSubstitute strings... :)

There are many paths to reach the same goal and we all choose how we get there... 8-)

Glad to hear you found your way :thumbup:
True, I was just hoping for one regex instead of having to split it. Thanks.