import re
regex = re.compile(r"^subject:\s(.+)", flags=re.IGNORECASE | re.MULTILINE)
test_str = ("Received: from DOMAIN.mydomain.com (UnknownHost [127.0.0.1]) by DOMAIN.mydomain.net with SMTP;\n"
" Fri, 6 Sep 2013 10:34:07 -0600\n"
"Date: Fri, 6 Sep 2013 10:34:07 -0600 (MDT)\n"
"From: \"MyDomain.com\" \n"
"To: test@anotherdomain.com\n"
"Message-ID: <8279725.100.1378485247161.JavaMail.MYDOMAIN$@127.0.0.1>\n"
"Subject: Membership Activation\n"
"MIME-Version: 1.0\n"
"Content-Type: text/html; charset=UTF-8\n"
"Content-Transfer-Encoding: 7bit\n"
"Subject: Membership Activation\n")
match = regex.search(test_str)
if match:
print(f"Match was found at {match.start()}-{match.end()}: {match.group()}")
for group_num, group in enumerate(match.groups(), start=1):
print(f"Group {group_num} found at {match.start(group_num)}-{match.end(group_num)}: {group}")
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html