It is currently July 4th, 2022, 5:52 am

A little help from RegEx wizards, if I may? Substituting one string for another

Get help with creating, editing & fixing problems with skins
jn_meter
Posts: 103
Joined: December 27th, 2016, 12:04 pm

A little help from RegEx wizards, if I may? Substituting one string for another

Post by jn_meter »

Hi all

I'm using the following measure, from here (and I am grateful to the original author).

Code: Select all

[MeasureName1]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=600
Url="http://time.yandex.com/sync.json?lang=en&geo=#Location1#"
Debug=1
RegExp="(?siU).*"name":"(.*)".*"offset":(.*),"offsetString":"(.*)""
StringIndex=1
DecodeCharacterReference=1
DynamicVariables=1
Substitute="":"Updating.Yandex"
Disabled=1
FinishAction=[!EnableMeasure MeasureName2][!UpdateMeasure MeasureName2]
; Each location WebParser measure is disabled on load. Then each is enabled, when the previous one is finished. 
; This prevents multiple WebParser measure from running simultaneously, which could lock up the WebParser.dll
There is already some regEx / substitution wizardry going on. What I'd like is to add some extra wizardry to the following effect. (So, the existing wizardry is something I'd like to preserve; I want to augment it.) When the measure returns Pacific/Auckland (exactly like that), I'd like to substitute the following shorter text: Auckland. I don't understand regEx / substitution myself - so could someone help me out, please?
User avatar
balala
Rainmeter Sage
Posts: 14338
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: A little help from RegEx wizards, if I may? Substituting one string for another

Post by balala »

jn_meter wrote: June 22nd, 2022, 12:38 am There is already some regEx / substitution wizardry going on. What I'd like is to add some extra wizardry to the following effect. (So, the existing wizardry is something I'd like to preserve; I want to augment it.) When the measure returns Pacific/Auckland (exactly like that), I'd like to substitute the following shorter text: Auckland. I don't understand regEx / substitution myself - so could someone help me out, please?
Regular expression substitution may indeed look like a wizardry, especially for beginners, but it's not.
Try to replace the Substitute option with the following two options:

Code: Select all

[MeasureName1]
...
RegExpSubstitute=1
Substitute="(.*)/(.*)":"\2","^$":"Updating.Yandex"
RegExpSubstitute=1 is needed to tell Rainmeter to make regular expression substitution (which is not done by default). This measn that Rainmeter will identify what is described below.
If you take a look to the Substitute option (RegExpSubstitute=1
Substitute="(.*)/(.*)":"\2","^$":"Updating.Yandex"
) you'll see the followings:
  • If the measure in which these options are added returns two strings separated by a slash (/), these two strings are represented by the two (.*) instances. Each of these expressions are representing any number of characters (dot . means one character, the * means any number). The first expression (the red one above) is identified in the right part of the option as \1, while the second expression (the green one) as \2. So what the Substitute option does is to identify the two slash separated strings and return the second one. Just to see how these options are working try to replace the \2 in the Substitute option with \1, in which case the measure will return Pacific, this being the first part of the string returned by the not-substitute measure.
  • To get working the existing Substitute option as well, I added the ,"^$":"Updating.Yandex" part to the previously described Substitute option. This had to be added to get replaced the empty string (represented by ^$, in which ^ means the beginning of the value returned by the measure and $ means its end - if these two are one after the other, means the string is empty) by Updating.Yandex, probably while the WebParser gets the value.
Note one more: even if a while ago WebParser was indeed a plugin, in meantime it became an internal measure and should be used as such. So replace the Measure=Plugin and Plugin=Plugins\WebParser.dll options of the measure by the simpler Measure=WebParser.
jn_meter
Posts: 103
Joined: December 27th, 2016, 12:04 pm

Re: A little help from RegEx wizards, if I may? Substituting one string for another

Post by jn_meter »

balala

Thank you for help. My skin now works as I wish it to. I appreciate the explanation that you gave, though in parts I struggled to understand it. I will try to remember to revisit that explanation if I need to delve into regEx substitution in the future.
User avatar
SilverAzide
Rainmeter Sage
Posts: 1923
Joined: March 23rd, 2015, 5:26 pm

Re: A little help from RegEx wizards, if I may? Substituting one string for another

Post by SilverAzide »

jn_meter wrote: June 22nd, 2022, 12:47 pm balala

Thank you for help. My skin now works as I wish it to. I appreciate the explanation that you gave, though in parts I struggled to understand it. I will try to remember to revisit that explanation if I need to delve into regEx substitution in the future.
If you just wanted the exact answer to your question, then you don't really need regex at all. Balala gave you the proper answer to a question you didn't really ask, which was how to do this correctly with regexs.

If you wanted to literally swap one string for another, per your original question, all you need is a very simple Substitute="this":"that". That will replace the string "this" with "that". This is shown in the docs as well. You can chain substitutions too, so using your existing code, you'd do something like Substitute="":"Updating.Yandex","Pacific/Auckland":"Auckland". But this is not the proper way to do it.

Balala's information is how to properly remove the portion of the string you didn't want, which is the question you probably meant to ask, and his info works for every timezone, not just the single one you mentioned.
Gadgets Wiki GitHub More Gadgets...
User avatar
balala
Rainmeter Sage
Posts: 14338
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: A little help from RegEx wizards, if I may? Substituting one string for another

Post by balala »

jn_meter wrote: June 22nd, 2022, 12:47 pm My skin now works as I wish it to. I appreciate the explanation that you gave, though in parts I struggled to understand it. I will try to remember to revisit that explanation if I need to delve into regEx substitution in the future.
Glad if you got it working as expected. Please come back if you have any question related to this problem (or finally to any other).
SilverAzide wrote: June 22nd, 2022, 2:27 pm If you just wanted the exact answer to your question, then you don't really need regex at all. Balala gave you the proper answer to a question you didn't really ask, which was how to do this correctly with regexs.

If you wanted to literally swap one string for another, per your original question, all you need is a very simple Substitute="this":"that". That will replace the string "this" with "that". This is shown in the docs as well. You can chain substitutions too, so using your existing code, you'd do something like Substitute="":"Updating.Yandex","Pacific/Auckland":"Auckland". But this is not the proper way to do it.

Balala's information is how to properly remove the portion of the string you didn't want, which is the question you probably meant to ask, and his info works for every timezone, not just the single one you mentioned.
Thanks for the appreciations. Mean a lot...