# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?<timestamp>\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d{3}Z) (?<level>\w+) (?<servicename>(\w+|\-))\[(?<uniquething>[A-F\d]+)\] \[(?<function>Originator@\d+) sub=(?<subsys>\S+)\] (?<body>.*)$"
test_str = ("2017-05-04T14:46:44.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=Default] Logging uses fast path: false\n"
"2017-05-04T14:46:45.163Z Section for PyVsanMOPropProvider, pid=68004, version=0.1, build=5310541, option=Release\n"
"2017-05-04T14:46:46Z mark: storage-path-claim-completed\n"
"2017-05-04T14:46:47.163Z verbose vsansystem[7AFFC9DB20] [Originator@6876 sub=Default] Dumping early logs:\n"
"------ Early init logs start --------\n"
"2017-05-04T14:46:47.163Z info -[7AFFC9DB20] [Originator@6876 sub=Default] Glibc malloc guards disabled.\n"
"2017-05-04T14:46:47.163Z info -[7AFFC9DB20] [Originator@6876 sub=Default] Initialized SystemFactory\n"
"------ Early init logs end --------\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=Default] Logging uses fast path: false\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=Default] The bora/lib logs WILL be handled by VmaCore\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=Default] Initialized channel manager\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=Default] Current working directory: /var/log/vmware\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=ThreadPool] Catch work item exceptions disabled.\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=FairScheduler] Priority level 4 is now active.\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=WorkQueue.vmacoreDefaultIOCompletionQueue] Created: WorkQueue.vmacoreDefaultIOCompletionQueue, type = fair, priority = 4 , itemWeight = 1\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=FairScheduler] Priority level 8 is now active.\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=WorkQueue.vmacoreDefaultIOQueue] Created: WorkQueue.vmacoreDefaultIOQueue, type = fair, priority = 8 , itemWeight = 1\n"
"2017-05-04T14:46:47.163Z info vsansystem[7AFFC9DB20] [Originator@6876 sub=FairScheduler] Priority level 16 is now active.")
matches = re.finditer(regex, test_str, re.MULTILINE)
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