It is currently March 28th, 2024, 6:43 pm

Rainmeter Regular expression behaving differently to expected

Get help with creating, editing & fixing problems with skins
Post Reply
DigitalEssence
Posts: 27
Joined: January 29th, 2016, 6:43 pm

Rainmeter Regular expression behaving differently to expected

Post by DigitalEssence »

Hi,

I have a reg ex that doesn't work as I expect it to in Rainmeter but does in 2 online reg ex tools.

The string is:

Code: Select all

<observation_time_rfc822>Thu, 11 Jan 2018 16:30:01 +0000</observation_time_rfc822>
and the regex is to pull out the time from this string in the format of xx:xx:xx

Code: Select all

(?siU)<observation_time_rfc822>.*?(\d{1,2}:\d{1,2}:\d{1,2}).*?</observation_time_rfc822>
In Rainmeter and the Fabulous RainRegExp tool it is giving me:

Code: Select all

6:30:0
But in this online tool: https://regex101.com/r/gMHSt4/4 it works as I expect (I may well be wrong as I usually am)

It also works as I expect here: https://regexr.com/3j3f7 and outputs:

Code: Select all

16:30:01
I have a workaround and that is to change the {1,2} to {2} which is probably a better option as the time format will, I assume always have two digits but should {1,2} not match correctly in Rainmeter?

thanks,

Hedley
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: Rainmeter Regular expression behaving differently to expected

Post by SilverAzide »

When I try your example on regex101.com it agrees with Rainmeter, not your other sites.

I think the key is this: {1,2} Quantifier — Matches between 1 and 2 times, as few times as possible, expanding as needed (lazy)

So since 1 digit matched, that's what it returned... :) Also, using ".*?" (greedy) instead of ".*" (lazy) changes things too.
Last edited by SilverAzide on January 11th, 2018, 5:27 pm, edited 1 time in total.
DigitalEssence
Posts: 27
Joined: January 29th, 2016, 6:43 pm

Re: Rainmeter Regular expression behaving differently to expected

Post by DigitalEssence »

SilverAzide wrote:When I try your example on regex101.com it agrees with Rainmeter, not your other sites.

I think the key is this: {1,2} Quantifier — Matches between 1 and 2 times, as few times as possible, expanding as needed (lazy)

So since 1 digit matched, that's what it returned... :)
Hi SilverAzide,

thanks for your reply.

I see what you mean if I set it to lazy then it matches with Rainmeter.
User avatar
FreeRaider
Posts: 826
Joined: November 20th, 2012, 11:58 pm

Re: Rainmeter Regular expression behaving differently to expected

Post by FreeRaider »

DigitalEssence wrote:Hi,

I have a reg ex that doesn't work as I expect it to in Rainmeter but does in 2 online reg ex tools.

The string is:

Code: Select all

<observation_time_rfc822>Thu, 11 Jan 2018 16:30:01 +0000</observation_time_rfc822>
and the regex is to pull out the time from this string in the format of xx:xx:xx

Code: Select all

(?siU)<observation_time_rfc822>.*?(\d{1,2}:\d{1,2}:\d{1,2}).*?</observation_time_rfc822>
In Rainmeter and the Fabulous RainRegExp tool it is giving me:

Code: Select all

6:30:0
But in this online tool: https://regex101.com/r/gMHSt4/4 it works as I expect (I may well be wrong as I usually am)

It also works as I expect here: https://regexr.com/3j3f7 and outputs:

Code: Select all

16:30:01
I have a workaround and that is to change the {1,2} to {2} which is probably a better option as the time format will, I assume always have two digits but should {1,2} not match correctly in Rainmeter?

thanks,

Hedley
apart from that you use /g (that is the global flag in https://regex101.com/r/gMHSt4/4 and in rainmeter it does not work) ... but simply put \s+ after ")"

(?siU)<observation_time_rfc822>.*?(\d{1,2}:\d{1,2}:\d{1,2})[color=#FF0000]\s+[/color].*?</observation_time_rfc822>
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Rainmeter Regular expression behaving differently to expected

Post by balala »

DigitalEssence wrote:The string is:

Code: Select all

<observation_time_rfc822>Thu, 11 Jan 2018 16:30:01 +0000</observation_time_rfc822>
and the regex is to pull out the time from this string in the format of xx:xx:xx

Code: Select all

(?siU)<observation_time_rfc822>.*?(\d{1,2}:\d{1,2}:\d{1,2}).*?</observation_time_rfc822>
I also would like to add three things, beside what SilverAzide and FreeRaider said:
  • Why have you put those question marks there? First I'd remove them ((?siU)<observation_time_rfc822>.*[color=#FF0000]?[/color](\d{1,2}:\d{1,2}:\d{1,2}).*[color=#FF0000]?[/color]</observation_time_rfc822>).
  • The second step would be to add a space both before and after the matching string (this is practically the same solution as FreeRaider's one).
  • And a last recommendation would be to avoid the need os having after the observation_time string, the rfc822 string. Not knowing your source and how does it behaves, I'm not sure, but would say there the string can change. That's why I'd use there a .*.
All these being said, here is my propose on the RegExp: RegExp=(?siU)<observation_time.*>.* (\d{1,2}:\d{1,2}:\d{1,2}) .*</observation_time.*>.
See the space between the <observation_time.*>.* and the matched numbers, respectively between those numbers and the final .*</observation_time.*>. Using FreeRaider's solution, this would be: RegExp=(?siU)<observation_time.*>.*\s+(\d{1,2}:\d{1,2}:\d{1,2})\s+.*</observation_time.*>.
DigitalEssence
Posts: 27
Joined: January 29th, 2016, 6:43 pm

Re: Rainmeter Regular expression behaving differently to expected

Post by DigitalEssence »

balala wrote:I also would like to add three things, beside what SilverAzide and FreeRaider said:
  • Why have you put those question marks there? First I'd remove them ((?siU)<observation_time_rfc822>.*[color=#FF0000]?[/color](\d{1,2}:\d{1,2}:\d{1,2}).*[color=#FF0000]?[/color]</observation_time_rfc822>).
  • The second step would be to add a space both before and after the matching string (this is practically the same solution as FreeRaider's one).
  • And a last recommendation would be to avoid the need os having after the observation_time string, the rfc822 string. Not knowing your source and how does it behaves, I'm not sure, but would say there the string can change. That's why I'd use there a .*.
All these being said, here is my propose on the RegExp: RegExp=(?siU)<observation_time.*>.* (\d{1,2}:\d{1,2}:\d{1,2}) .*</observation_time.*>.
See the space between the <observation_time.*>.* and the matched numbers, respectively between those numbers and the final .*</observation_time.*>. Using FreeRaider's solution, this would be: RegExp=(?siU)<observation_time.*>.*\s+(\d{1,2}:\d{1,2}:\d{1,2})\s+.*</observation_time.*>.
Hi,

the question mark is there as it makes it not-greedy.

Or does in Perl. As far as I know...

The source xml has the following options for the time:

Code: Select all

<observation_time>Last Updated on January 12, 4:49 PM GMT</observation_time>
<observation_time_rfc822>Fri, 12 Jan 2018 16:49:59 +0000</observation_time_rfc822>
<observation_epoch>1515775799</observation_epoch>
<local_time_rfc822>Fri, 12 Jan 2018 16:53:01 +0000</local_time_rfc822>
<local_epoch>1515775981</local_epoch>
Whether this may change or not I don't know.

Thanks for your updates. I will run through them and update my skin.

Thanks as always.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Rainmeter Regular expression behaving differently to expected

Post by balala »

Ok, let us know if the posted solutions did work.
User avatar
SilverAzide
Rainmeter Sage
Posts: 2588
Joined: March 23rd, 2015, 5:26 pm
Contact:

Re: Rainmeter Regular expression behaving differently to expected

Post by SilverAzide »

DigitalEssence wrote:Whether this may change or not I don't know.
It will not, which is nice. The reference in the tag to "rfc822" means that this is a timestamp formatted per RFC-822. You can safely parse this without worrying about a format change.
DigitalEssence
Posts: 27
Joined: January 29th, 2016, 6:43 pm

Re: Rainmeter Regular expression behaving differently to expected

Post by DigitalEssence »

balala wrote:Ok, let us know if the posted solutions did work.
Yes, the solution posted works:

Code: Select all

.*\s+(\d{1,2}:\d{1,2}:\d{1,2})\s+.*
And I was hoping that because it was formatted to rfc822 that I was safe but in the short period I have been active on this forum, I'm learning not to assume anything ;-)

Thanks for your help. Resolved.
User avatar
balala
Rainmeter Sage
Posts: 16110
Joined: October 11th, 2010, 6:27 pm
Location: Gheorgheni, Romania

Re: Rainmeter Regular expression behaving differently to expected

Post by balala »

Glad to help.
Post Reply