Advertisement
go6odn28

Extract_emails_and_name

Oct 28th, 2023
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.68 KB | None | 0 0
  1. #Extract emails:
  2.  
  3. import re
  4.  
  5. text = "Just send email to s.miller@mit.edu and j.hopking@york.ac.uk for more information."
  6. pattern = r"(^|(?<=\s))([a-z0-9]+[\.\-\_a-z]*)@([a-z\-]+)(\.[a-z]+)+\b"
  7. matches = re.finditer(pattern, text)
  8.  
  9. list_with_emails = []
  10. list_with_names = []
  11. for match in matches:
  12.     email = match.group()
  13.     username = match.group(2)
  14.     list_with_names.append(username)
  15.     list_with_emails.append(email)
  16.     start_index = match.start()
  17.     end_index = match.end()
  18.     print(f"Email: {email}")
  19.     print(f"Start Index: {start_index}")
  20.     print(f"End Index: {end_index}")
  21.     print()
  22.  
  23. print(*list_with_emails, sep=", ")
  24. print(*list_with_names, sep=", ")
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement