-2

I have a string as:

string="""Recipient:
nolo.lamb@golab.com \--- mail_boundary --- ATTENTION: This email came from an
external source.

Sender: nlrt@vloh.net Subject: [External] *LEGALZOOM OPENS AT $30, IPO
AT $28 Message-Id: <60DC94E60001AE8432F70080_0_2129298@mscv03>
Recipient: Nichole.wen@golab.com \--- mail_boundary --- """

All I want to extract the email-ID's corresponding to keyword Recipient: i.e. email_id=['nolo.lamb@golab.com','Nichole.wen@golab.com']

What I have tried as:

email_id=re.findall(r'Recipient: (.+)',string)
Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
Learner
  • 800
  • 1
  • 8
  • 23
  • 1
    refer here:https://stackoverflow.com/questions/17681670/extract-email-sub-strings-from-large-document/17681902 – gretal Dec 08 '21 at 07:00
  • @gretal Already checked. It gives all the email ID's in string. I want only those which is corresponding to keyword `Recipient:` – Learner Dec 08 '21 at 07:31
  • Then just replace the `(.+)` from your pattern with what they give as a valid email pattern – Tomerikoo Jan 31 '23 at 14:53

1 Answers1

0

You can use

re.findall(r'\bRecipient:\s*(\S+@\S+)', text)

See the regex demo. Details:

  • \b - word boundary
  • Recipient: - a fixed string
  • \s* - zero or more whitespaces
  • (\S+@\S+) - Group 1: one or more non-whitespace chars, @ and one or more non-whitespace chars

See the Python demo:

import re
text = """Recipient:
nolo.lamb@golab.com \--- mail_boundary --- ATTENTION: This email came from an
external source.
 
Sender: nlrt@vloh.net Subject: [External] *LEGALZOOM OPENS AT $30, IPO
AT $28 Message-Id: <60DC94E60001AE8432F70080_0_2129298@mscv03>
Recipient: Nichole.wen@golab.com \--- mail_boundary --- """
 
print( re.findall(r'\bRecipient:\s*(\S+@\S+)', text) )
# => ['nolo.lamb@golab.com', 'Nichole.wen@golab.com']
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563