# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"""
^\QJob started\E
(?:(?!\QJob ended\E).)+?
^\QJob executed successfully\E
"""
test_str = ("Job started at '0902 23:56:00:367' orderno - '0tzh0' runno - '00064' Number of transfers - 1\n\n"
"Host1 'Local'[Windows-LOCAL] username 'xxx\\xxx' - Host2 'xxx.xxx.xx'[Unix-SFTP] username 'xxx'\n\n"
"Local host is: xxx - Windows 200x [601] Service Pack 1 build 7601 - Intel64 Family 6 Model 37 Stepping 1, GenuineIntel\n\n"
"********** Starting transfer #1 out of 1 *************** Transfer #1 completed successfully\n\n"
"Job executed successfully. exiting.\n\n"
"Job ended at '0902 23:56:07:138' Elapsed time [7sec] CPU usage [0.15sec]\n\n"
"Job started at '0831 15:26:00:365' orderno - '0tuq5' runno - '00030' Number of transfers - 4\n\n"
"Host1 'Local'[Windows-LOCAL] username 'xxx\\xxx' - Host2 'xxx.xxx.xx'[Unix-SFTP] username 'xxx'\n\n"
"Local host is: xxx - Windows 200x [601] Service Pack 1 build 7601 - Intel64 Family 6 Model 37 Stepping 1, GenuineIntel\n\n"
"********** Starting transfer #1 out of 4 *************** Unable to connect to SSH server on 'xxx.xxx.xx': SFTP_Connect : psftp_connect failed : ssh_init: Network error: Connection timed out .\n\n"
"Connection to host sftp.onenet.be could not be established\n\n"
"Job ended at '0831 15:26:21:426'\n\n"
"Elapsed time [21sec] CPU usage [0.0sec]\n")
matches = re.finditer(regex, test_str, re.MULTILINE | re.VERBOSE | re.DOTALL)
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