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?
It is currently May 6th, 2024, 6:40 pm
Reg x question.
-
- Posts: 185
- Joined: December 4th, 2009, 2:05 am
-
- Developer
- Posts: 22631
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Reg x question.
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.
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.
-
- Posts: 185
- Joined: December 4th, 2009, 2:05 am
Re: Reg x question.
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.
-
- Developer
- Posts: 22631
- Joined: April 19th, 2009, 11:02 pm
- Location: Fort Hunt, Virginia, USA
Re: Reg x question.
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.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.
-
- Posts: 185
- Joined: December 4th, 2009, 2:05 am
Re: Reg x question.
@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.
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
-
- Developer
- Posts: 2872
- Joined: April 17th, 2009, 12:18 pm
Re: Reg x question.
Assuming that the larger image always has width="144", you could use:
If the width varies, you'd need to match a range of numbers. Let me know if the above is enough.
Code: Select all
(?siU)<media:thumbnail (?(?=.*width="144").*width="144").*url="(.*)"
-
- Posts: 185
- Joined: December 4th, 2009, 2:05 am
Re: Reg x question.
@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.
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.
-
- Developer
- Posts: 2872
- Joined: April 17th, 2009, 12:18 pm
Re: Reg x question.
(?(?=.*width="144").*width="144") in plain English would be: If width="144" is found ahead (the (?=..) part), skip everything until it.
-
- Posts: 185
- Joined: December 4th, 2009, 2:05 am
Re: Reg x question.
Got it.Thanks again.