It is currently September 14th, 2024, 10:09 pm

[Webparser] Making part of a regex optional

Get help with creating, editing & fixing problems with skins
ak_
Posts: 4
Joined: October 10th, 2010, 6:12 pm

[Webparser] Making part of a regex optional

Post by ak_ »

Hi, i'm new to Rainmeter and this regex thing is making me crazy.

As a first project, i'm trying to make a simple Remember the milk reader (i know there is one already, but i'm trying to make my own as an exercise).

I'm using the Webparser plugin to parse my RTM atom feed in order to display 5 tasks. My regex is like this :

Code: Select all

RegExp="(?siU)<title type="html">.*</title>.*<title type="html">(.*)</title>.*<title type="html">(.*)</title>.*<title type="html">(.*)</title>.*<title type="html">(.*)</title>.*<title type="html">(.*)</title>"
The problem is : if there are less than 5 tasks in my RTM account, this regex doesn't work and i get a matching error. So my question is : how can i make parts of my regex optional ? I read the regex documentation about optional matches and tried something like this (to make last match optional) :

Code: Select all

RegExp="(?siU)<title type="html">.*</title>.*<title type="html">(.*)</title>.*<title type="html">(.*)</title>.*<title type="html">(.*)</title>.*<title type="html">(.*)</title>(.*<title type="html">(.*)</title>)?"
But it doesn't work.

I tried to look at the source of the existing RTM reader but i didn't understand how the regex worked.

Any help would be greatly appreciated, i awfully need to get things done :)
Last edited by ak_ on October 11th, 2010, 10:40 am, edited 1 time in total.
poiru
Developer
Posts: 2873
Joined: April 17th, 2009, 12:18 pm

Re: [Webparser] Making part of a regex optional

Post by poiru »

.*(?(?=.*<title type="html">).*<title type="html">(.*)</tit)

That will get one title if it exists. Add 5 of those in a row.

Wrote this on my phone, so might not work. Lemme know if it doesn't, and I'll take a closer look.
ak_
Posts: 4
Joined: October 10th, 2010, 6:12 pm

Re: [Webparser] Making part of a regex optional

Post by ak_ »

It works ! Thanks a lot.

The sad thing is i don't quite understand it and why it works :(

The (?= part is a positive lookahead ?
User avatar
jsmorley
Developer
Posts: 22743
Joined: April 19th, 2009, 11:02 pm
Location: Fort Hunt, Virginia, USA

Re: [Webparser] Making part of a regex optional

Post by jsmorley »

ak_ wrote:It works ! Thanks a lot.

The sad thing is i don't quite understand it and why it works :(

The (?= part is a positive lookahead ?
http://www.regular-expressions.info/lookaround.html
ak_
Posts: 4
Joined: October 10th, 2010, 6:12 pm

Re: [Webparser] Making part of a regex optional

Post by ak_ »

Thanks. I know lookarounds, but i don't understand the structure of this particular regex. For example, what's the (? part for ?
poiru
Developer
Posts: 2873
Joined: April 17th, 2009, 12:18 pm

Re: [Webparser] Making part of a regex optional

Post by poiru »

To put it simply:

If <titleblaa is on the right then <titleblaa(.*)...
ak_
Posts: 4
Joined: October 10th, 2010, 6:12 pm

Re: [Webparser] Making part of a regex optional

Post by ak_ »

I think i get it.

Thank you very much :)