Home > front end >  re.findall() not returning any results
re.findall() not returning any results

Time:08-20

I'm trying to parse my calendar entries in python. I've written an AppleScript that will grab the calendar entries I want and return them as a string. An example...

BEGIN:VCALENDAR
VERSION:2.0
PRODID:-//Microsoft Corporation//Outlook for Mac MIMEDIR//EN
METHOD:PUBLISH
BEGIN:VTIMEZONE
TZID:Dublin, Edinburgh, Lisbon, London
X-ENTOURAGE-CFTIMEZONE:Europe/London
X-ENTOURAGE-TZID:8
BEGIN:STANDARD
RRULE:FREQ=YEARLY;INTERVAL=1;BYSECOND=0;BYMINUTE=0;BYHOUR=2;BYDAY=-1SU;BYM
 ONTH=10
TZOFFSETFROM: 0100
TZOFFSETTO: 0000
DTSTART:16011001T020000
END:STANDARD
BEGIN:DAYLIGHT
RRULE:FREQ=YEARLY;INTERVAL=1;BYSECOND=0;BYMINUTE=0;BYHOUR=1;BYDAY=-1SU;BYM
 ONTH=3
TZOFFSETFROM: 0000
TZOFFSETTO: 0100
DTSTART:16010301T010000
END:DAYLIGHT
END:VTIMEZONE
BEGIN:VEVENT
...

I have this stored in a string cal_string.

I now want to extract each of the VTIMEZONE elements in to it's own string and so am using re.findall() however it is returning no results...

my_timezones = re.findall(r'BEGIN:VTIMEZONE(.*?)END:VTIMEZONE', cal_string)
print("timezones: ", my_timezones)

This just prints timezones: []

I've tried with/without the preceeding r in the findall and that made no difference. I've also validated the expression by running on test_string = 'BEGIN:VTIMEZONE test test test END:VTIMEZONE' and that correctly returned test test test.

CodePudding user response:

You need to use the re.DOTALL flag, without it, . matches anything but newline (demo)

my_timezones = re.findall(r'BEGIN:VTIMEZONE(.*?)END:VTIMEZONE', cal_string, flags=re.DOTALL)

Or inlined (demo)

my_timezones = re.findall(r'(?s)BEGIN:VTIMEZONE(.*?)END:VTIMEZONE', cal_string)
  • Related