It is currently April 24th, 2024, 4:10 am

Regex

Get help with creating, editing & fixing problems with skins
User avatar
jsmorley
Developer
Posts: 22629
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Regex

Post by jsmorley »

As may come as no surprise, I would be tempted to jump to Lua for this:

Code: Select all

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

[Variables]
MyGoodVar=20,20,20|10,10,10|20,20,20
MyBadVar1=20,20,20|20,20,20|20,20,20
MyBadVar2=10,10,10|10,10,10|20,20,20

[Lua]
Measure=Script
ScriptFile=Test.lua
Disabled=1

[MeterResult]
Meter=String
FontSize=12
FontWeight=400
FontColor=255,255,255,255
SolidColor=47,47,47,255
Padding=5,5,5,5
AntiAlias=1
Text=MyGoodVar is: [Lua:ColorTest('#MyGoodVar#')]#CRLF#MyBadVar1 is: [Lua:ColorTest('#MyBadVar1#')]#CRLF#MyBadVar2 is: [Lua:ColorTest('#MyBadVar2#')]
DynamicVariables=1
Test.Lua:

Code: Select all

function ColorTest(inArg)

	splitBits = {}
	
	for oneBit in string.gmatch(inArg, '[^|]+') do
		table.insert(splitBits, oneBit)
	end

	if splitBits[1] == '20,20,20' and splitBits[2] ~= '20,20,20' and splitBits[3] == '20,20,20' then
		return 1
	else
		return -1
	end

end
1.jpg

This creates an "iterator" that marches through the string, capturing any bits that are not "|" (our separator) and sticking them into a table. This "splits" the string on the separator, into the table. Then we can test the fields in the table for "==" (equal) and "~=" (not equal) to determine our result.

You can use this result any way you want, for instance in an IfCondition on a measure to then use bangs to change something. My example simply displays the result.

Whether you use this for this particular need or not, feel free to throw a blizzard of measures at it instead, knowing how to "split" a string on a separator in Lua is a very useful thing to have in your toolbelt.
You do not have the required permissions to view the files attached to this post.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

Thanks for all your answers, I used lua after all because it was the most simple choice.

Now I'm trying to do this:

Code: Select all

[SomeMeasure]
Measure=WebParser
URL=<a itemprop="url" href="OtherStuff">OtherSutff</a>
RegExp="(?siU)<a itemprop="url" href="(.*)">(.*)</a>"
StringIndex=1
DynamicVariables=1
But obviously this isn't a URL so I can't parse it...
How can I do this?
I tried:

Code: Select all

[SomeMeasure]
Measure=String
String=<a itemprop="url" href="OtherStuff">OtherSutff</a>
Substitute="(?siU)<a itemprop="url" href="(.*)">(.*)</a>"
StringIndex=1
DynamicVariables=1
RegExpSubstitute=1
But that isn't really a substitute so that didn't work either...
User avatar
balala
Rainmeter Sage
Posts: 16164
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Regex

Post by balala »

kyriakos876 wrote: November 8th, 2018, 7:48 pm But that isn't really a substitute so that didn't work either...
Use the String measure, not the WebParser. In that one, your Substitute option is not complete: a such option must have two sections: a pattern and a replacement. Yours has just one: Substitute="(?siU)<a itemprop="url" href="(.*)">(.*)</a>". There is no colon, nor a replacement section. But even if there would be, the option still wouldn't work, because you have quotation signs in pattern. Exactly for this, you shouldn't have to delimit the pattern with quotes, but with single quotes.
This being said, here is a completed and working Substitute option: Substitute='(?siU)<a itemprop="url" href="(.*)">(.*)</a>':"\1"
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

balala wrote: November 8th, 2018, 8:04 pm Use the String measure, not the WebParser. In that one, your Substitute option is not complete: a such option must have two sections: a pattern and a replacement. Yours has just one: Substitute="(?siU)<a itemprop="url" href="(.*)">(.*)</a>". There is no colon, nor a replacement section. But even if there would be, the option still wouldn't work, because you have quotation signs in pattern. Exactly for this, you shouldn't have to delimit the pattern with quotes, but with single quotes.
This being said, here is a completed and working Substitute option: Substitute='(?siU)<a itemprop="url" href="(.*)">(.*)</a>':"\1"
I want to get the values inside the "Stuff" and "OtherStuff" strings if I do this:

Code: Select all

[ToolTipLinkForPost1]
Measure=String
String=<a itemprop="url" href="Stuff">OtherStuff</a>
Substitute='(?siU)<a itemprop="url" href="(.*)">(.*)</a>':"\1"
StringIndex=1
DynamicVariables=1
RegExpSubstitute=1
I don't get either...
Sorry if I understood something wrong here.
User avatar
balala
Rainmeter Sage
Posts: 16164
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Regex

Post by balala »

kyriakos876 wrote: November 8th, 2018, 8:10 pm I want to get the values inside the "Stuff" and "OtherStuff" strings if I do this:

Code: Select all

[ToolTipLinkForPost1]
Measure=String
String=<a itemprop="url" href="Stuff">OtherStuff</a>
Substitute='(?siU)<a itemprop="url" href="(.*)">(.*)</a>':"\1"
StringIndex=1
DynamicVariables=1
RegExpSubstitute=1
I don't get either...
Sorry if I understood something wrong here.
Sorry, I think I didn't understand what do you want to get: Stuff or OtherStuff? If you want the Stuff, use as replacement the "\1", but if you want OtherStuff, use "\2".
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

balala wrote: November 8th, 2018, 8:22 pm Sorry, I think I didn't understand what do you want to get: Stuff or OtherStuff? If you want the Stuff, use as replacement the "\1", but if you want OtherStuff, use "\2".
Ah yes... Thanks you I understand now. It works as I want to! :great:
User avatar
balala
Rainmeter Sage
Posts: 16164
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Regex

Post by balala »

kyriakos876 wrote: November 8th, 2018, 8:52 pm Ah yes... Thanks you I understand now. It works as I want to! :great:
I'm glad if it does.
However, there are two other things, to be added:
  • The StringIndex option isn't needed (nor valid) on the [ToolTipLinkForPost1] String measure. Remove it (I suppose you've copied the options from the WebParser measure and you left / forgot it there).
  • Nor the DynamicVariables=1 option isn't needed on the measure. Remove it as well.
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

balala wrote: November 8th, 2018, 9:00 pm I'm glad if it does.
However, there are two other things, to be added:
  • The StringIndex option isn't needed (nor valid) on the [ToolTipLinkForPost1] String measure. Remove it (I suppose you've copied the options from the WebParser measure and you left / forgot it there).
  • Nor the DynamicVariables=1 option isn't needed on the measure. Remove it as well.
Yes, I removed the StringIndex I forgot it as you said.
The dynamic variable is needed actually because the actual string comes from a measure.
Here's the whole thing if you care:

Code: Select all

[ParentPost]
Measure=Plugin
Plugin=WebParser
URL=#ParentURL#
RegExp=#ParentRegExp#
FinishAction=[!EnableMeasure NotificationCreator][!UpdateMeasure NotificationCreator]
DynamicVariables=1

[LinkForPost1]
Measure=WebParser
URL=[ParentPost]
StringIndex=(3*1-1)
DynamicVariables=1

[ToolTipParentForPost1]
Measure=WebParser
URL=[&LinkForPost1]
RegExp="(?siU)<p>(.*)</p>"
StringIndex=1
Substitute="<br />":""
DynamicVariables=1
DecodeCharacterReference=1

[ToolTipLinkForPost1]
Measure=String
String=[ToolTipParentForPost1]
Substitute='(?siU)<a itemprop="url" href="(.*)">(.*)</a>':"\1"
DynamicVariables=1
RegExpSubstitute=1

[ToolTipForPost1]
Measure=String
String=[ToolTipParentForPost1]
Substitute='(?siU)<a itemprop="url" href="(.*)">(.*)</a>':"\2"
DynamicVariables=1
RegExpSubstitute=1
It's a little bit complicated, but if you know what it's doing is simple :D

Thanks again! :)
User avatar
balala
Rainmeter Sage
Posts: 16164
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Regex

Post by balala »

kyriakos876 wrote: November 8th, 2018, 9:06 pm The dynamic variable is needed actually because the actual string comes from a measure.
In this case it indeed is needed.
But I can't try out the code, because I don't know nor the ParentURL, nor the ParentRegExp variable, both used into the URL, respectively RegExp options of the [ParentPost] measure. But if it does work, it's ok, I think.
Just one more small, not extremely important thing: there is no need to include any option into quotes, even if there is / are spaces into that option. I'm talking for example about the RegExp option of the [ToolTipParentForPost1] measure. Details: https://forum.rainmeter.net/viewtopic.php?f=5&t=26350&p=137635&hilit=required+setoption+option#p137635
User avatar
kyriakos876
Posts: 919
Joined: January 30th, 2017, 2:01 am
Location: Greece

Re: Regex

Post by kyriakos876 »

balala wrote: November 8th, 2018, 9:22 pm In this case it indeed is needed.
But I can't try out the code, because I don't know nor the ParentURL, nor the ParentRegExp variable, both used into the URL, respectively RegExp options of the [ParentPost] measure. But if it does work, it's ok, I think.
Just one more small, not extremely important thing: there is no need to include any option into quotes, even if there is / are spaces into that option. I'm talking for example about the RegExp option of the [ToolTipParentForPost1] measure. Details: https://forum.rainmeter.net/viewtopic.php?f=5&t=26350&p=137635&hilit=required+setoption+option#p137635
Yea, the ParentURL is in weird so I didn't share to avoid unnecessary headaches. As you said, if it works it's ok, which it does.

About the quotes, I'm just spamming them wherever I can, pure out of habit :)