It is currently April 27th, 2024, 5:33 am

Help on regular expressions

Get help with creating, editing & fixing problems with skins
User avatar
haibusa2005
Posts: 52
Joined: June 15th, 2011, 7:23 pm
Location: Bulgaria, Varna

Help on regular expressions

Post by haibusa2005 »

Hi, everyone! I am trying to make a clock skin with a button that changes its appearance depending on the sunrise and sunset times, which means it's one image before sunrise and another after sunrise. I am parsing this page with a regular expression in order to get the sunrise and sunset times. This is the regular expression I am using

Code: Select all

RegExp="(?siU).*<sunr>(.*)</sunr>.*<suns>(.*)</suns>.*"
The return strings are in format "hh:mm AM/PM" which is great for just displaying, but I need a to separate the hours, the minutes and the AM/PM string so I can compare them to local time. I am really bad at regular expressions and have no idea how to parse the time components one by one.

Full code bellow
[Rainmeter]
Author=Haibusa2005
AppVersion=0.01
Update=1000
Blur=1
BlurRegion=1,0,0,330,240

[Metadata]
Description=Weather Clock
License=Creative Commons BY-NC-SA 3.0
Version=0.01

[Variables]
Location=BUXX0001
Metric=m

[GetDay]
Measure=Time
Format=%A
Substitute="Monday":"Ïîíåäåëíèê","Tuesday":"Âòîðíèê","Wednesday":"Ñðÿäà","Thursday":"×åòâúðòúê","Friday":"Ïåòúê","Saturday":"Ñúáîòà","Sunday":"Íåäåëÿ"

[GetDate]
Measure=Time
Format=%d %B
Substitute="January":"ßíóàðè","February":"Ôåâðóàðè","March":"Ìàðò","April":"Àïðèë","May":"Ìàé","June":"Þíè","July":"Þëè","August":"Àâãóñò","September":"Ñåïòåìâðè","October":"Îêòîìâðè","November":"Íîåìâðè","December":"Äåêåìâðè"

[GetTime]
Measure=Time
Format=%H:%M:%S

[GetAngleHours]
Measure=Time
Format=%H

[GetAngleMinutes]
Measure=Time
Format=%M

[AngleMinutes]
Measure=Calc
Formula=GetAngleMinutes/1200

[SetAngle]
Measure=Calc
Formula=GetAngleHours=22?-0.05-[AngleMinutes]:(GetAngleHours=23?-0.1-[AngleMinutes]:(GetAngleHours=00?-0.15-[AngleMinutes]:(GetAngleHours=1?-0.20-[AngleMinutes]:(GetAngleHours=2?-0.2-[AngleMinutes]:(GetAngleHours=3?-0.15-[AngleMinutes]:(GetAngleHours=4?-0.1-[AngleMinutes]:(GetAngleHours=5?-0.05-[AngleMinutes]:0)))))))
DynamicVariables=1

[StayingLateHours]
Measure=Calc
Formula=GetAngleHours=23?11111:(GetAngleHours=00?11111:(GetAngleHours=1?11111:(GetAngleHours=2?11111:22222)))
Substitute="11111":"255,255,255,160","22222":"255,255,255,1"
DynamicVariables=1

[GoodMorningHours]
Measure=Calc
Formula=GetAngleHours=7?11111:(GetAngleHours=8?11111:(GetAngleHours=9?11111:22222))
Substitute="11111":"255,255,255,192","22222":"255,255,255,1"
DynamicVariables=1

[Frame]
Meter=Image
ImageName=bkg.png
x=0
y=0
H=240
W=330

[DisplayDay]
Meter=String
MeasureName=GetDay
x=300
y=0
FontSize=40
FontColor=255,255,255,192
AntiAlias=1
StringAlign=Right
FontFace=Calibri

[DisplayDate]
Meter=String
MeasureName=GetDate
x=300
y=44
FontSize=60
FontColor=255,255,255,192
AntiAlias=1
StringAlign=Right
FontFace=FreeSans

[DisplayTime]
Meter=String
MeasureName=GetTime
x=80
y=160
FontSize=40
FontColor=255,255,255,192
AntiAlias=1
FontFace=Calibri
Angle=[SetAngle]
DynamicVariables=1

[StayingLate]
Meter=String
x=330
y=210
W=330
H=28
FontSize=16
StringAlign=Right
AntiAlias=1
FontColor=[StayingLateHours]
FontFace=FreeSans
Text="Íå å ëè âðåìå çà ñúí?"
DynamicVariables=1

[GoodMorning]
Meter=String
x=5
y=135
FontSize=20
AntiAlias=1
StringAlign=Left
FontColor=[GoodMorningHours]
FontFace=FreeSans
Text="Äîáðî óòðî!"
DynamicVariables=1

;Weather related stuff goes here

[RegExpCurrentWeather]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=3000
Url=http://xml.weather.com/weather/local/#Location#?cc=*&unit=#Metric#&dayf=0
;RegExp="(?siU).*<locale>(.*)</locale>.*<ut>(.*)</ut>.*<ud>(.*)</ud>.*<us>(.*)</us>.*<up>(.*)</up>.*<ur>(.*)</ur>.*<loc id="(.*)">.*<dnam>(.*)</dnam>.*<tm>(.*)</tm>.*<lat>(.*)</lat>.*<lon>(.*)</lon>.*<sunr>(.*)</sunr>.*<suns>(.*)</suns>.*<zone>(.*)</zone>.*<cc>.*<lsup>(.*)</lsup>.*<obst>(.*)</obst>.*<tmp>(.*)</tmp>.*<flik>(.*)</flik>.*<t>(.*)</t>.*<icon>(.*)</icon>.*<bar>.*<r>(.*)</r>.*<d>(.*)</d>.*<wind>.*<s>(.*)</s>.*<gust>(.*)</gust>.*<d>(.*)</d>.*<t>(.*)</t>.*<hmid>(.*)</hmid>.*<vis>(.*)</vis>.*<uv>.*<i>(.*)</i>.*<t>(.*)</t>.*<dewp>(.*)</dewp>.*<moon>.*<icon>(.*)</icon>.*<t>(.*)</t>.*"

;isolated regexp
RegExp="(?siU).*<sunr>(.*)</sunr>.*<suns>(.*)</suns>.*"
Debug=1

[RegExpTodayWeather]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=3000
Url=http://xml.weather.com/weather/local/#Location#?cc=*&unit=#Metric#&dayf=1
RegExp="(?siU).*<locale>(.*)</locale>.*<ut>(.*)</ut>.*<ud>(.*)</ud>.*<us>(.*)</us>.*<up>(.*)</up>.*<ur>(.*)</ur>.*<loc id="(.*)">.*<dnam>(.*)</dnam>.*<tm>(.*)</tm>.*<lat>(.*)</lat>.*<lon>(.*)</lon>.*<sunr>(.*)</sunr>.*<suns>(.*)</suns>.*<zone>(.*)</zone>.*<cc>.*<lsup>(.*)</lsup>.*<obst>(.*)</obst>.*<tmp>(.*)</tmp>.*<flik>(.*)</flik>.*<t>(.*)</t>.*<icon>(.*)</icon>.*<bar>.*<r>(.*)</r>.*<d>(.*)</d>.*<wind>.*<s>(.*)</s>.*<gust>(.*)</gust>.*<d>(.*)</d>.*<t>(.*)</t>.*<hmid>(.*)</hmid>.*<vis>(.*)</vis>.*<uv>.*<i>(.*)</i>.*<t>(.*)</t>.*<dewp>(.*)</dewp>.*<moon>.*<icon>(.*)</icon>.*<t>(.*)</t>.*"
;Debug=1

[RegExpTomorrowWeather]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=3000
Url=http://xml.weather.com/weather/local/#Location#?cc=*&unit=#Metric#&dayf=2
RegExp="(?siU)<day d="1" t="(.*)" dt="(.*)".*<hi>(.*)</hi>.*<low>(.*)</low>.*<sunr>(.*)</sunr>.*<suns>(.*)</suns>.*<part p="d">.*<icon>(.*)</icon>.*<t>(.*)</t>.*<wind>.*<s>(.*)</s>.*<gust>(.*)</gust>.*<d>(.*)</d>.*<t>(.*)</t>.*<bt>(.*)</bt>.*<ppcp>(.*)</ppcp>.*<hmid>(.*)</hmid>.*<part p="n">.*<icon>(.*)</icon>.*<t>(.*)</t>.*<wind>.*<s>(.*)</s>.*<gust>(.*)</gust>.*<d>(.*)</d>.*<t>(.*)</t>.*<bt>(.*)</bt>.*<ppcp>(.*)</ppcp>.*<hmid>(.*)</hmid>.*"
;Debug=1

[GetSunrise]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpCurrentWeather]
StringIndex=12

[GetSunset]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpCurrentWeather]
StringIndex=13

[GetCity]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpCurrentWeather]
StringIndex=16

[GetCurrentWeather]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpCurrentWeather]
StringIndex=19

[GetCurrentTemp]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpCurrentWeather]
StringIndex=17

[GetCurrentTempFeelsLike]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpCurrentWeather]
StringIndex=18

[GetCurrentWind]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpCurrentWeather]
StringIndex=23

[GetTodayWeather]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpTodayWeather]
StringIndex=19
;Substitute=#subs#

[GetTodayTemp]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpTodayWeather]
StringIndex=17

[GetTodayWind]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpTodayWeather]
StringIndex=23

[GetTomorrowWeather]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpTomorrowWeather]
StringIndex=8
;Substitute=#subs#

[GetTomorrowTempMax]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpTomorrowWeather]
StringIndex=3

[GetTomorrowTempMin]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpTomorrowWeather]
StringIndex=4

[GetTomorrowWind]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[RegExpTomorrowWeather]
StringIndex=9

[GetWeatherTime]
Measure=Time
Format=%I

[GetWeatherTimeMarker]
Measure=Time
Format=%p
Substitute="PM":"1","AM":"0"
User avatar
Mordasius
Posts: 1173
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: Help on regular expressions

Post by Mordasius »

The RegExp (?siU).*<sunr>(.*):(.*)\s.*</sunr>.*<suns>(.*):(.*)\s. will yield:

String Index 01=> 5
String Index 02=> 44
String Index 03=> 8
String Index 04=> 47

So sunrise is 5:44 and sunset is 8:47. I don't suppose you need the AM and PM bits.

I suggest you hurry on over to this post to grab a copy of JSMorley's Rainmeter Regular Expression tester and team it up with this reference sheet to take the pain out of getting RegExps to work properly.

Thanks to JS for the recent update to stop RainRegExp being so "greedy". :thumbup:
User avatar
haibusa2005
Posts: 52
Joined: June 15th, 2011, 7:23 pm
Location: Bulgaria, Varna

Re: Help on regular expressions

Post by haibusa2005 »

Thank you, works like a charm :) The program of JSMorley is perfect for mastering the regular expressions