It is currently May 2nd, 2024, 12:44 pm

Question concerning Regular Expressions

Get help with creating, editing & fixing problems with skins
Fulmar
Posts: 21
Joined: July 24th, 2012, 4:50 pm
Location: Belgium

Question concerning Regular Expressions

Post by Fulmar »

Hello,

The actual difference between Coordinated Universal Time and International Atomic Time is indicated on the last line of http://maia.usno.navy.mil/ser7/tai-utc.dat.

For a skin I am writing concerning times I want to use the "35.0", following TAI-UTC= on the last line.

So the search should go till the end, and then it should take the information following the last expression "TAI-UTC=". But how do I write this in regular expressions?

Many thanks :)
User avatar
MerlinTheRed
Rainmeter Sage
Posts: 889
Joined: September 6th, 2011, 6:34 am

Re: Question concerning Regular Expressions

Post by MerlinTheRed »

If you are really and absolutely sure that it will always be the number on the last line, you can use look-ahead expressions to make sure what you find is the last occurrence of TAI-UTC. You do this by saying "find me an occurrence of TAI-UTC that has no other occurrences of TAI-UTC after it".

It would look something like this:

(?si)TAI-UTC(?!.*TAI-UTC)\s*=\s*(\d+\.\d*)

(?si) means: be case-insensitive and treat the whole file as a single line. If not using "s" The regex will match each line separately, outputting a new StringIndex for each line (at least that's what RainRegExp suggests. I haven't tried it in Rainmeter myself)
.* means: any character, any number of occurrences
(?!...) matches only at a position where the expression in the braces is not found
(?!.*TAI-UTC) means, match at a position where no occurrence of TAI-UTC is found after it
TAI-UTC(?!.*TAI-UTC) means, find TAI-UTC that has no other TAI-UTC after it
\s*=\s* means, some whitespace, a = and some whitespace
(\d+\.\d*) means, one or more digits, a dot, and zero or more digits. This is not a very robust regex for a floating point number but if your numbers will always look like in the example it will work. The braces around the expression mean that it is captured and can be accessed via StringIndex.

In fact, you can simplify this expression:

(?si).*TAI-UTC\s*=\s*(\d+\.\d*)

When not using "U" in the first braces, an asterisk will match as much as possible. Therefore, the first .* matches as much as it can to make the whole expression match. This means that all occurrences but the last of "TAI-UTC" are skipped, because there is still something more that can be added without making the expression fail. When using "U", an asterisk will match as little as possible, which means the first occurrence of "TAI-UTC" would be found.
Have more fun creating skins with Sublime Text 2 and the Rainmeter Package!
Fulmar
Posts: 21
Joined: July 24th, 2012, 4:50 pm
Location: Belgium

Re: Question concerning Regular Expressions

Post by Fulmar »

Thank you for the advice. I got the result I wanted to have, i.e. "35.0".

But now I noticed I cannot calculate with the result. Is it possible "35.0" isn't recognized as a number?

:(
User avatar
MerlinTheRed
Rainmeter Sage
Posts: 889
Joined: September 6th, 2011, 6:34 am

Re: Question concerning Regular Expressions

Post by MerlinTheRed »

It might be you have to pipe it through a Calc measure first. E.g. create a calc measure that has Formula=WebParserMeasureYourNumberComesFrom. This will parse the string and convert it to a number. You can then use the Calc measure if you need to calculate something with the number.
Have more fun creating skins with Sublime Text 2 and the Rainmeter Package!
Fulmar
Posts: 21
Joined: July 24th, 2012, 4:50 pm
Location: Belgium

Re: Question concerning Regular Expressions

Post by Fulmar »

Thank you.

Furthermore I put the Measure's name between brackets, and I made the measure dynamic, like this:

[DifferenceInternationalAtomicTime]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateDivider=-1
ForceReload=1
Url=file://#CURRENTPATH#DownloadFile\Tai.xml
RegExp="(?si)TAI-UTC(?!.*TAI-UTC)\s*=\s*(\d+\.\d*)"
StringIndex=1

[NumberInternationalAtomicTime]
Measure=Calc
Formula=[DifferenceInternationalAtomicTime]
DynamicVariables=1

[JulianDayInternationalAtomicTime]
Measure=Calc
Formula=JulianDayCoordinatedUniversalTime+NumberInternationalAtomicTime/86400

Thanks for your kind help,

:bow:
User avatar
MerlinTheRed
Rainmeter Sage
Posts: 889
Joined: September 6th, 2011, 6:34 am

Re: Question concerning Regular Expressions

Post by MerlinTheRed »

Normally you shouldn't need the brackets in a formula of a calc measure.
Have more fun creating skins with Sublime Text 2 and the Rainmeter Package!
User avatar
KreAch3R
Posts: 608
Joined: February 7th, 2011, 7:27 pm
Location: Thessaloniki, GR

Re: Question concerning Regular Expressions

Post by KreAch3R »

You would need them if you wanted to do this transformation (from string to number). But that shouldn't be the case anymore, he can use the new Section Variables; [DifferenceInternationalAtomicTime:] should return the string as a number.
Inactive, due to life changes. Send me a PM for any question.

Desktop DeviartArt
Image
User avatar
MerlinTheRed
Rainmeter Sage
Posts: 889
Joined: September 6th, 2011, 6:34 am

Re: Question concerning Regular Expressions

Post by MerlinTheRed »

If they also work for measures that are considered to only have a string value, this is really nice.
Have more fun creating skins with Sublime Text 2 and the Rainmeter Package!