Python Regex to extract meeting invite from Gmail Subject

Question:

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) ([email protected])

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 🙂

Many thanks!

Asked By: Cummins070

||

Answers:

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) ([email protected])'
)
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]).

Answered By: JRiggles

It looks like you don’t technically need regex for this.

Try the following:

>>> s = 'Invitation: Bob / Carol Meeting @ Tue Oct 25, 2022 11:30am - 12pm (CST) ([email protected])'
>>> s[s.index('@') + 1 : s.rindex('(')].strip()
'Tue Oct 25, 2022 11:30am - 12pm (CST)'
Answered By: rv.kvetch
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.