Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python Regex to extract meeting invite from Gmail Subject

I’m trying to extract the meeting date / time from meeting invites within Gmail’s subject. Below is an example of a subject for a meeting invite:

Invitation: Bob / Carol Meeting @ Tue Oct 25, 2022 11:30am - 12pm (CST) (bob@example.org)

What I would like to extract:

Tue Oct 25, 2022 11:30am - 12pm (CST)

I think the pattern could simply start with the space after the "@" and end with the ")". My Regex is very rusty so would appreciate any help 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Many thanks!

>Solution :

Try this – it should match everything after the "@ " and up to the end of the timezone ")"

import re

string = (
    'Invitation: Bob / Carol Meeting @ Tue Oct 25, 2022 11:30am - 12pm (CST) (bob@example.org)'
)
pattern = re.compile(r'(?<=@ )[^)]+\)')
matches = re.findall(pattern, string)
print(matches)
# => 'Tue Oct 25, 2022 11:30am - 12pm (CST)'

See here for a breakdown of the RegEx I used. Bear in mind that re.findall returns a list of matches, which is helpful if you want to scan a long multiline string of text and get all the matches at once. If you only care about the 1st match, you can get it by index e.g. print(matches[0]).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading