# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"^(?<hostip>[^ ]+) - - \[(?<timestamp>[^ ]+)\s-(?<unknown1>\d+)] \"(?<method>[^ ]+) (?<request>[^ ]+) HTTP\/\d\.\d\" (?<response>\d+) - (?<bytes>\d+)"
test_str = ("10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /iconic/staticContentBuild/js/jquery-1.8.2.js HTTP/1.1\" 200 - 580 SMUSER=- remote-client-ip=10.29.110.51 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n\n"
"10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /retail/staticContentBuild/js/pos-module.js HTTP/1.1\" 200 - 120 SMUSER=- remote-client-ip=10.164.96.28 http_referer=- Agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET CLR 1.1.4322; .NET4.0C; .NET4.0E)\n\n"
"10.170.63.247 - - [16/Jun/2015:10:25:56 -0400] \"GET /retail/mobilepos/screens/ETR2/styles/omnilink.css HTTP/1.1\" 200 - 170 SMUSER=- remote-client-ip=10.27.174.140 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n\n"
"10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/staticContentBuild/images/queue_icon_greyw.png HTTP/1.1\" 200 - 230 SMUSER=- remote-client-ip=10.167.154.165 http_referer=- Agent=Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; Tablet PC 2.0; .NET CLR 1.1.4322)\n\n"
"10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/staticContentBuild/js/handlebar-data.js HTTP/1.1\" 200 - 140 SMUSER=- remote-client-ip=10.166.12.56 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n\n"
"10.144.62.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /retail/mobilepos/screens/ETR2/scripts/templates/device-details/modals/reset-voicemail-password/resetVoicemailPasswordItemView.tmpl HTTP/1.1\" 200 - 180 SMUSER=- remote-client-ip=10.130.4.66 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n\n"
"10.170.63.247 - - [16/Jun/2015:10:25:57 -0400] \"GET /iconic/staticContentBuild/js/accountLines.js HTTP/1.1\" 200 - 240 SMUSER=- remote-client-ip=10.164.18.46 http_referer=- Agent=Mozilla/5.0 (iPad; CPU OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Mobile/11D257\n")
matches = re.search(regex, test_str)
if matches:
print ("Match was found at {start}-{end}: {match}".format(start = matches.start(), end = matches.end(), match = matches.group()))
for groupNum in range(0, len(matches.groups())):
groupNum = groupNum + 1
print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = matches.start(groupNum), end = matches.end(groupNum), group = matches.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