It is currently May 3rd, 2024, 4:11 am

How to extract a number from a string?

Get help with creating, editing & fixing problems with skins
TugaMan
Posts: 12
Joined: June 25th, 2012, 9:40 am

How to extract a number from a string?

Post by TugaMan »

I have the following code below. The meter [MeterSunrise] returns a string from a plugin (e.g. "06:27 AM"). Is it possible to extract specific number values from this string, so they could be use in a Calc meter (e.g. extract the values "06" and "27")?

Code: Select all

[MeasureWeather]
Measure=Plugin
Plugin=Plugins\WebParser.dll
UpdateRate=900
Url=http://xml.weather.com/weather/local/#LocationCode#?cc=*&unit=m&dayf=2
RegExp="(?siU).*<locale>(.*)</locale> ...

[MeasureSunrise]
Measure=Plugin
Plugin=Plugins\WebParser.dll
Url=[MeasureWeather]
StringIndex=12

[MeterSunrise]
MeasureName=MeasureSunrise
Meter=STRING
X= ...
Y= ...
User avatar
Mordasius
Posts: 1173
Joined: January 22nd, 2011, 4:23 pm
Location: GMT +8

Re: How to extract a number from a string?

Post by Mordasius »

TugaMan wrote: Is it possible to extract specific number values from this string, so they could be use in a Calc meter (e.g. extract the values "06" and "27")?
I think you can do that by replacing <sunr>(.*)</sunr> with <sunr>(.*):(.*)\s.*</sunr> in the RegExp= you have in [MeasureWeather]. Your StringIndex=12 will then give the hour of the Sunrise and StringIndex=13 will give the minutes of the Sunrise. These can then be used in Calc Measures.

Don't forget that all the StringIndex= numbers greater than 12 will have been increased by one (1) for all subsequent measures.
TugaMan
Posts: 12
Joined: June 25th, 2012, 9:40 am

Re: How to extract a number from a string?

Post by TugaMan »

Thanks Mordasius... Worked perfectly! :thumbup: