It is currently May 6th, 2024, 6:40 pm

Reg x question.

Get help with creating, editing & fixing problems with skins
User avatar
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Reg x question.

Post by Jkon »

I've have a skin I'm using to read bbc news feeds.It gets description,link and image.When it comes to the image,the bbc feed normally offers 2 options,

<media:thumbnail width="66" height="49" url="http://news.bbcimg.co.uk/media/images/53962000/jpg/_53962238_012402448-1.jpg"/>
<media:thumbnail width="144" height="81" url="http://news.bbcimg.co.uk/media/images/53962000/jpg/_53962116_012396830-1.jpg"/>

I want, and can, extract the 2nd (larger) image but sometimes there is only the first (smaller) option.Is it possible to make an expression look for the 2nd image but resort to the first if the 2nd does not exist?
Image
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Reg x question.

Post by jsmorley »

You could do a "look ahead assertion" in the Regular Expression to get both without it failing if one doesn't exist, which I sort of demonstrate here: http://forum.rainmeter.net/viewtopic.php?f=5&t=8005&p=48336&hilit=assertion#p48336. Then you are probably going to want to play a trick with Substitute= to replace a child WebParser measure returning "" with -1 or something. That way you later use an IfBelowAction to hide or show the appropriate image meter.

The other option is to have WebParser dump the html to a file, then use a Lua script to parse it. That way you can easily use if / then / else processing in the Lua to get the image name you want, and just return it in a variable to Rainmeter that you use with ImageName=#ImageVar# in the image meter with DynamicVariables=1.
User avatar
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Re: Reg x question.

Post by Jkon »

I'll have a crack at the first option,thanks JS. I havent a clue about Lua and am not going to take it on at the moment,still dealing with trying to understand regular expressions.
Image
User avatar
jsmorley
Developer
Posts: 22631
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: Reg x question.

Post by jsmorley »

Jkon wrote:I'll have a crack at the first option,thanks JS. I havent a clue about Lua and am not going to take it on at the moment,still dealing with trying to understand regular expressions.
Understood. Fair enough. Look Ahead Assertions can be a bit daunting at first, but if you search for that phrase on google there are some good examples / guides out there, and a couple of us here can help if you get stuck.
User avatar
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Re: Reg x question.

Post by Jkon »

@jsmorely

Well, I've been reading the chapter on lookarounds athttp://www.regular-expressions.info/tutorial.html and I have to say WOW!!!!, thats some head melting stuff. While still trying to come to terms with that i read ahead to "Continuing matches" and I am wondering if you think combining a lookaround with perls "magical" \G could be a solution, as, if my understanding is correct this will cause the engine to reread the string if the first match attempt fails.
If a match attempt fails, the stored position for \G is reset to the start of the string
Image
poiru
Developer
Posts: 2872
Joined: April 17th, 2009, 12:18 pm

Re: Reg x question.

Post by poiru »

Assuming that the larger image always has width="144", you could use:

Code: Select all

(?siU)<media:thumbnail (?(?=.*width="144").*width="144").*url="(.*)"
If the width varies, you'd need to match a range of numbers. Let me know if the above is enough.
User avatar
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Re: Reg x question.

Post by Jkon »

@poiru

RegX =(?(?=.*<item).*<description>(.*)</description>.*<link>(.*)</link>.*<media:thumbnail (?(?=.*width="144").*width="144").*url="(.*)"/>)

RegExp="(?siU)<ttl>.*#RegX##RegX##RegX##RegX##RegX#"

The above seems to be working great,thank you.Am I right in thinking that (?(?=.*width="144").*width="144") is a lookahead within a lookahead and that because the second lookahead fails the engine is backtracking to .*url="(.*)"

The width does appear to be a constant value on all their feeds but I am going to try to edit the regex to accommodate any size greater then the first,just for the sake of learning how to do it.Looking through the tutorial,it seems easy enough.
Image
poiru
Developer
Posts: 2872
Joined: April 17th, 2009, 12:18 pm

Re: Reg x question.

Post by poiru »

(?(?=.*width="144").*width="144") in plain English would be: If width="144" is found ahead (the (?=..) part), skip everything until it.
User avatar
Jkon
Posts: 185
Joined: December 4th, 2009, 2:05 am

Re: Reg x question.

Post by Jkon »

Got it.Thanks again.
Image