import re
regex = re.compile(r"^(?<time>[^ ]* {1,2}[^ ]* [^ ]*) (?<ident>[a-zA-Z0-9_\/\.\-]*)(?:\[(?<pid>[0-9]+)\])?(?:[^\:]*\:)? \[(?<time_server>[^\]]*)\] (?<host>[^ ]*) - (?<user>[^ ]*)\\\"(?<method>\S+)(?: +(?<path>[^\"]*) +\S*)?\" (?<code>[^ ]*) (?<size>[^ ]*) \\\"(?<referer>[^\"]*)\\\" \\\"(?<agent>[^\"]*)\\\"rt=(?<request_time>[^ ]*) uct=\\\"(?<upstream_connect_time>[^ ]*)\\\" uht=\\\"(?<upstream_header_time>[^ ]*)\\\" urt=\\\"(?<upstream_response_time>[^ ]*)\\\" uaddr=\\\"(?<upstream_addr>[^ ]*)\\\" x-forwarded-for=\\\"*(?<http_x_forwarded_for>.*)\\\"$")
test_str = "Jun 26 06:53:39 00f628825635[3156]: [26/Jun/2018:06:53:39 +0000] 10.66.44.27 - -\\\"GET /health_check HTTP/1.1\\\" 200 0 \\\"-\\\" \\\"ELB-HealthChecker/1.0\\\"rt=0.000 uct=\\\"-\\\" uht=\\\"-\\\" urt=\\\"-\\\" uaddr=\\\"-\\\" x-forwarded-for=\\\"-\\\""
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