# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"([\w-]+?)=(\".*?\"|\S+)"
test_str = ("appName=XXX clientIp=X.X.X timestamp=\"2017-06-05T13:22:12-07:00\" request=\"POST /forward HTTP/1.1\" statusCode=204 bytesOut=1167 totalTime=0.062 bytesIn=1289 sourceHost=XXXX connId=49936598 connReqs=9 upInstance=XXX:104:XXX-XXX:8664:17F34 upConnectSec=0.052 upAddr=\"XX.XX.XX:123\" upHost=\"vcv08it-cvcv2801:8464\" upHdrTimeSec=0.058 upRespTimeSec=0.058 pid=32561 upStatusCode=204 message=\"Access Log\" corrKey=GMIFCDIKRZR2T4VZQXJA2IT6 upCached=- length=0 partition=XXX location=\"= /v1/tXXXX\" xff=\"XX.XX.XX.XX\" referer=\"-\" user-agent=\"Apache-HttpAsyncClient/4.1.1 (Java/1.8.0_131)\\\" rateLimitCurrentValues=\"--\" rateLimitTimeMs=\\\"-:-\"\n")
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