import re
regex = re.compile(r"to=<(?P<email>\S+)>,\srelay=(?!10\.10\.10\.).+dsn=(?P<dsn>\d+\.\d+\.\d+),\sstatus=(?!sent)")
test_str = ("2016-10-17T00:00:00.028777+03:00 postfix/smtp[6628]: 878A13DCA5: to=<govorok@gmail.com>, relay=gmail-smtp-in.l.google.com[173.194.220.27]:25, delay=0.47, delays=0.01/0/0.12/0.35, dsn=2.0.0, status=sent (250 2.0.0 OK 1476651600 79si16679285lja.36 - gsmtp)\n"
"2016-10-17T00:00:00.029273+03:00 postfix/qmgr[4674]: 878A13DCA5: removed\n"
"2016-10-17T00:00:00.037122+03:00 postfix/smtpd[6697]: 0906DA5: client=unknown[192.168.2.74]\n"
"2016-10-17T00:00:00.039621+03:00 postfix/cleanup[6713]: 0906DA5: message-id=<20161016210000.0906DA5@mrelay-h3.hh.ru>\n"
"2016-10-17T00:00:00.040403+03:00 postfix/smtp[6690]: C5670B9: to=<daler_rajabov@inbox.ru>, relay=mxs.mail.ru[217.69.139.150]:25, delay=0.23, delays=0.01/0/0.02/0.2, dsn=2.0.0, status=1s3ent (250 OK id=1bvsXH-0003Cb-Rp)")
matches = regex.finditer(test_str)
for match_num, match in enumerate(matches, start=1):
print(f"Match {match_num} 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