I need to grab specific details being parsed in from email bodies, in this case the emails are plain text and formatted like so:
imbad@regex.com
John Doe
+16073948374
2021-04-27T15:38:11+0000
14904
The above is an example output of print(body) parsed in from an email like so:
def parseEmail(popServer, msgNum):
raw_message=popServer.retr(msgNum)[1]
str_message=email.message_from_bytes(b'\n'.join(raw_message))
body=str(str_message.get_payload())
So, if I needed to simply grab the email address and phone number from body object, how might I do that using regex?
I understand regex is most certainly overkill for this, however I'm only repurposing an existing in-house utility that's already written to utilize regex for more complex queries, so it seems the simplest solution here would to modify the regex to grab the desired text. attempts to utilize str.partition() resulted in other unrelated errors.
Thank you in advance.
You could use the following regex patterns:
For the email: \.+@.+\n/g
For the phone number: \^[+]\d+\n/gm
Remove the Initial forward slash if using in python re library.
Note in the email one only the global flag is used, but for the phone number pattern, the multiline flag is also used.
Simply loop over every body, capturing these details and storing them how you like.