# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^t=(?<longtime>[^ ]+)\s*lvl=(?<loglevel>[^ ]+)\s*msg=(?<msg>(?:\"[^\"]+\"|[^ ]+))\s*logger=(?<logger>[^ ]+)\s*(?:addresses=(?<email>(?:\"\[[^\"]+\]\"|[^ ]+)))?(?:userId=(?<userID>[^ ]+))?\s*(?:orgId=(?<orgID>[^ ]+))?\s*(?:uname=(?<uname>[^ ]+|[??]*))?\s*(?:method=(?<method>[^ ]+))?\s*(?:path=(?<path>(?:\"[^\"]+\"|[^ ]+)))?\s*(?:ruleId=(?<ruleID>[^ ]+))?\s*(?:name=(?<name>(?:\"[^\"]+\"|[^ ]+)))?\s*(?:error=(?<error>(?:\"[^\"]+\"|[^ ]+)))?\s*(?:changing state to=(?<ChStateTo>[^ ]+))?\s*(?:status=(?<status>[^ ]+))?\s*(?:remote_addr=(?<remote_addr>[^ ]+))?\s*(?:time_ms=(?<time_ms>[^ ]+))?\s*(?:size=(?<size>[^ ]+))?\s*(?:referer=(?<referer>(?:\"[^\"]+\"|[^ ]+)|[??]*))?\s*(?:alertId=(?<alertID>[^ ]+))?\s*(?:newState=(?<NewState>[^ ]+))?\s*(?:prev state=(?<prevState>[^ ]+))?\s*(?:url=(?<url>[^ ]+))?$"
test_str = ("t=2019-07-30T07:22:35+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=22 name=\"Выдача сломалась!\" error=\"Alert execution exceeded the timeout\" changing state to=alerting\n"
"t=2019-07-30T07:22:39+0300 lvl=info msg=\"New state change\" logger=alerting.resultHandler alertId=48 newState=alerting prev state=ok\n"
"t=2019-07-30T07:22:39+0300 lvl=info msg=\"uploaded screenshot of alert to external image store\" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/TylgxQWG4Aiq6kmlSrM4.png\n"
"t=2019-07-30T07:22:39+0300 lvl=info msg=\"uploaded screenshot of alert to external image store\" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/lsfc738fTMkR034AZM2j.png\n"
"t=2019-07-30T07:22:39+0300 lvl=info msg=\"Sending webhook\" logger=alerting.notifier.webhook\n"
"t=2019-07-30T07:22:39+0300 lvl=info msg=\"Sending webhook\" logger=alerting.notifier.webhook\n"
"t=2019-07-30T07:22:40+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=89 name=Вход.Формы.Статусы error=\"Alert execution exceeded the timeout\" changing state to=alerting\n"
"t=2019-07-30T07:22:40+0300 lvl=info msg=\"New state change\" logger=alerting.resultHandler alertId=89 newState=alerting prev state=ok\n"
"t=2019-07-30T07:22:40+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=88 name=\"Exception State alert\" error=\"Alert execution exceeded the timeout\" changing state to=alerting\n"
"t=2019-07-30T07:22:40+0300 lvl=info msg=\"uploaded screenshot of alert to external image store\" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/tQeLMXFNBU6y6USKOe5E.png\n"
"t=2019-07-30T07:22:40+0300 lvl=info msg=\"Sending webhook\" logger=alerting.notifier.webhook\n"
"t=2019-07-30T07:22:42+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=21 name=\"HermesApiErrors alert\" error=\"Alert execution exceeded the timeout\" changing state to=alerting\n"
"t=2019-07-30T07:22:42+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=26 name=\"Генерация отчетов на Jasper alert\" error=\"Alert execution exceeded the timeout\" changing state to=keep_state\n"
"t=2019-07-30T07:22:45+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=32 name=\"СРВО. Фоновые задачи\" error=\"Alert execution exceeded the timeout\" changing state to=alerting\n"
"t=2019-07-30T07:22:45+0300 lvl=info msg=\"New state change\" logger=alerting.resultHandler alertId=32 newState=alerting prev state=ok\n"
"t=2019-07-30T07:22:45+0300 lvl=info msg=\"uploaded screenshot of alert to external image store\" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/yZzqi4QmGCvSbYraq3UC.png\n"
"t=2019-07-30T07:22:45+0300 lvl=info msg=\"Sending webhook\" logger=alerting.notifier.webhook\n"
"t=2019-07-30T07:22:45+0300 lvl=info msg=\"Sending alert notification to\" logger=alerting.notifier.email addresses=\"[Andrey.Kudashkin@russianpost.ru Arkhipov.Alexander@russianpost.ru szhogin@luxoft.com OPodus@luxoft.com]\"\n"
"t=2019-07-30T07:22:47+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=135 name=\"pochtaid-sso alert\" error=\"Alert execution exceeded the timeout\" changing state to=alerting\n"
"t=2019-07-30T07:22:47+0300 lvl=info msg=\"New state change\" logger=alerting.resultHandler alertId=135 newState=alerting prev state=ok\n"
"t=2019-07-30T07:22:47+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=90 name=\"Профиль. Статус\" error=\"Alert execution exceeded the timeout\" changing state to=alerting\n"
"t=2019-07-30T07:22:47+0300 lvl=info msg=\"New state change\" logger=alerting.resultHandler alertId=90 newState=alerting prev state=ok\n"
"t=2019-07-30T07:22:47+0300 lvl=eror msg=\"Alert Rule Result Error\" logger=alerting.evalContext ruleId=136 name=\"Normalization alert\" error=\"Alert execution exceeded the timeout\" changing state to=alerting\n"
"t=2019-07-30T07:22:47+0300 lvl=info msg=\"New state change\" logger=alerting.resultHandler alertId=136 newState=alerting prev state=ok\n"
"t=2019-07-30T07:22:47+0300 lvl=info msg=\"uploaded screenshot of alert to external image store\" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/qXR85haxAmRX5g1vqrmT.png\n"
"t=2019-07-30T07:22:47+0300 lvl=info msg=\"uploaded screenshot of alert to external image store\" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/dOfYePDh7n3oreQhaRCR.png\n"
"t=2019-07-30T07:22:47+0300 lvl=info msg=\"Sending webhook\" logger=alerting.notifier.webhook\n"
"t=2019-07-30T07:22:47+0300 lvl=info msg=\"uploaded screenshot of alert to external image store\" logger=alerting.notifier url=http://10.233.60.59:8080/grafana/4eIoX3qCRuF16DoXNbRU.png\n")
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