fork download
  1. import re
  2. full_name_list=['John Doe', 'James Henry', 'Jane Doe']
  3. source = r" John Doe is part of our team and here is his email: johndoe@something.com. James Henry is also part of our team and here his email: jameshenry@something.com. Jane Doe is the team manager and you can contact her at that address: janedoe@something.com"
  4. for name in full_name_list:
  5. # full name followed by the email
  6. name_email = re.search(r'\b' + name+r'\b.*?([\w.-]+@[\w.-]+\.w+)', source)
  7. if name_email:
  8. print( 'Email before "{}" keyword: {}'.format(name, name_email.group(1)) )
  9. # email followed by full name
  10. email_name = re.search(r'\b([\w.-]+@[\w.-]+\.\w+)(?:(?![\w.-]+@[\w.-]+\.\w).)*?\b'+name+r'\b', source, re.S)
  11. if email_name:
  12. print( 'Email after "{}" keyword: {}'.format(name, email_name.group(1)) )
Success #stdin #stdout 0.02s 9420KB
stdin
Standard input is empty
stdout
Email after "James Henry" keyword: johndoe@something.com
Email after "Jane Doe" keyword: jameshenry@something.com