# coding=utf8
import re
regex = r"^(.*)\r?\n\s*\r?\nfrom:.*\r?\nsent:.*\r?\nto:.*\r?\nsubject:"
test_str = ("Hello World from: the other side of the first email\n\n"
"from: this\n"
"sent: at\n"
"to: that\n"
"subject: what\n\n"
"second email\n\n"
"from: this\n"
"sent: at\n"
"to: that\n"
"subject: what\n\n\n"
"third email\n\n"
"from: this\n"
"date: at\n"
"to: that\n"
"subject: what\n\n"
"fourth email")
matches = re.search(regex, test_str, re.MULTILINE)
if matches:
print (matches.group(1))
IyBjb2Rpbmc9dXRmOAoKaW1wb3J0IHJlCgpyZWdleCA9IHIiXiguKilccj9cblxzKlxyP1xuZnJvbTouKlxyP1xuc2VudDouKlxyP1xudG86Lipccj9cbnN1YmplY3Q6IgoKdGVzdF9zdHIgPSAoIkhlbGxvIFdvcmxkIGZyb206IHRoZSBvdGhlciBzaWRlIG9mIHRoZSBmaXJzdCBlbWFpbFxuXG4iCgkiZnJvbTogdGhpc1xuIgoJInNlbnQ6IGF0XG4iCgkidG86IHRoYXRcbiIKCSJzdWJqZWN0OiB3aGF0XG5cbiIKCSJzZWNvbmQgZW1haWxcblxuIgoJImZyb206IHRoaXNcbiIKCSJzZW50OiBhdFxuIgoJInRvOiB0aGF0XG4iCgkic3ViamVjdDogd2hhdFxuXG5cbiIKCSJ0aGlyZCBlbWFpbFxuXG4iCgkiZnJvbTogdGhpc1xuIgoJImRhdGU6IGF0XG4iCgkidG86IHRoYXRcbiIKCSJzdWJqZWN0OiB3aGF0XG5cbiIKCSJmb3VydGggZW1haWwiKQoKbWF0Y2hlcyA9IHJlLnNlYXJjaChyZWdleCwgdGVzdF9zdHIsIHJlLk1VTFRJTElORSkKCmlmIG1hdGNoZXM6CiAgICBwcmludCAobWF0Y2hlcy5ncm91cCgxKSk=