Home > Back-end >  Python Regex to extract meeting invite from Gmail Subject
Python Regex to extract meeting invite from Gmail Subject

Time:10-22

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!

CodePudding user response:

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]).

CodePudding user response:

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)'
  • Related