fork download
  1. # coding=utf8
  2.  
  3. import re
  4.  
  5. regex = r"^(.*)\r?\n\s*\r?\nfrom:.*\r?\nsent:.*\r?\nto:.*\r?\nsubject:"
  6.  
  7. test_str = ("Hello World from: the other side of the first email\n\n"
  8. "from: this\n"
  9. "sent: at\n"
  10. "to: that\n"
  11. "subject: what\n\n"
  12. "second email\n\n"
  13. "from: this\n"
  14. "sent: at\n"
  15. "to: that\n"
  16. "subject: what\n\n\n"
  17. "third email\n\n"
  18. "from: this\n"
  19. "date: at\n"
  20. "to: that\n"
  21. "subject: what\n\n"
  22. "fourth email")
  23.  
  24. matches = re.search(regex, test_str, re.MULTILINE)
  25.  
  26. if matches:
  27. print (matches.group(1))
Success #stdin #stdout 0.02s 9468KB
stdin
Standard input is empty
stdout
Hello World from: the other side of the first email