# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(by ).*(with)"
test_str = ("from s32.hekko.net.com\n"
"by s32.hekko.net.com with LMTP id 0KRkJkxRu1qDS1AAbRxtZA\n"
"for <john@gmail.com>; Wed, 28 Mar 2018 10:24:44 +0200\n"
"Return-path: <johnsfather@onet.com>\n"
"Envelope-to: john@gmail.com\n"
"Delivery-date: Wed, 28 Mar 2018 10:24:44 +0200\n\n"
"from smtpo114.poczta.onet.com ([213.180.149.147])\n"
"by s32.hekko.net.com with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256)\n"
"(Exim 4.90_1)\n"
"(envelope-from <johnsfather@onet.com>)\n"
"id 1f16Nv-00057D-Sj\n"
"for john@gmail.com; Wed, 28 Mar 2018 10:24:44 +0200\n\n"
"from pmq4v.m5r2.onet (pmq4v.m5r2.onet [30.174.32.60])\n"
"by smtp.poczta.onet.com (Onet) with ESMTP id 40B2Bw16y9zqtBg4\n"
"for <john@gmail.com>; Wed, 28 Mar 2018 10:24:40 +0200 (CEST)\n"
"DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=onet.com; s=2011;\n"
"t=1522235480; bh=6YEU3a1jv48GQ33zI1zre2Sf05w2DqLyMQhJ7UmYfyM=;\n"
"h=From:To:Date:Subject:From;\n"
"b=svf7rTDJFp7gUMQFA6GShG07bYFIpsSaxPOvdXYad/2ln6x+BGE4z4OoIfqBgpGhE\n"
"2n43jucpTRvw7mdT33vor7NXnI6mMXjLZjJp6J4cOpxGLnuAnSEo0G/xLWirIgFmm4\n"
"TojYv7tx5z/PugqZc/c59WzApn14FEhmVSrC+1IE=\n"
"Content-Type: multipart/alternative; boundary=\"===============2073168411==\"\n"
"MIME-Version: 1.0\n\n"
"from [37.47.10.237] by pma4v.m5r2.onet via HTTP id\n"
"201803281024458192010001; Wed, 28 Mar 2018 10:24:40 +0200")
matches = re.finditer(regex, test_str)
for matchNum, match in enumerate(matches, start=1):
print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))
for groupNum in range(0, len(match.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
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