# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?<src_ip>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})\s\D{1,5}(?<date_time>[^ ]+)\s(?<GMT_offset>[^ ]+)\s(?<action>[^ ]+.+a=)(?<hash_url>[^ ]+)\s(?<protocol>[a-zA-Z]+)[^ ]+\s(?<code>\d{1,3})\s(?<octet>[^ ]+)\s(?<ratio>[^ ]+)\W(?<percent_comp>[^ ]+)\W(?<dst_url>[^ ]+)\s(?<user_agent>[^ ]+.+)"
test_str = ("80.254.146.36 - - [11/Jun/2015:11:04:10 +0000] \"GET /alert/process?a=-3-kj8UgHY54dtgX8mZCfPUCRkiu-r_Kh1hnSqbe40rOjxSzUCVGJLfF_PiDhvXVCFsI3a8RMB5tiVZGzG_A0LowA&b=K1ky5h_rXTuqI2kqaBwFai5qnz5xw_pL_RrBcgfqJok-Rrw7d7i2aziQR53ekX-XFPMy2cECsNi1VNW5nIav_sB7MR1WAKQ2pUIc8ypRdROqnwChscBsbvBZb1zLy4e8kP1erE9mOPLoh6afcbOgcPGpeMuauYR8fulX9u5SonSwYN5ms02yd1Z07z9bPZVX9reGuDDJwigALp2gHCl6S8AEWU0sRsZJnadtRbyBOK81pw73OOqtb8JoRPk3415vx279PmJAVt6x3bm47F50yTebSjxAesEDvA9NYCkVWDJCQFppfpHjon2KeoDmdHLmEu9yEzA1dMDIdJCR_4Br8fEw5cqnly2_vUSuSbNvaxfD9OBWmD2X5CoD_6uobS6IW2EzmYJfVGVdOBUqW-QkgYIC2SDVszd1z2CSCix__jQxjllQPXBp0HL864KVErsdUytBL1J0LTJPJQOCBFaTZCGIpyw0EvPqSZ3S5dXi8SolRQm3YK_7SQVI3-4JTNuwzi3G-UeB_M4&blockedUrl=http%3A%2F%2Fad.turn.com%2Fserver%2Fads.js%3Fpub%3D5766351%26cch%3D5766918%26code%3D5766926%26l%3D728x90%26aid%3D35714892%26ahcid%3D11498647%26bimpd%3D%7E4loWx2hBbQDrGFCWDJjke1g42uW7RLTdWiesOx0b3B1KFjiJdGJoNfXmferT63OHUr9sDQYZFDbJH2q768FJZ5h8VG1agJSao30l5WNLKv98yrSWaIELfnH2EFg1BHn3Q8IoGll5sNh0d9FxmcEi9\n"
"--eSKj7A7w6cH9Uhu3Nm16AqSNNHfE4_5G_-9t-Ty8MARdvMjrN4H1TX56t3ZdWC9UlEQeIel1lknbmbr9Ki0BWJgEJ-IAqMeVM4yqSn7-2I7cC7e0MtWLbi-AQS39wHwwBttsHbCFeMUOo7uVI70hx392w1cRHX2Hf1Px-Q9fg9k0GTY4hk80E_fsRESHoIpJoCa5Sh7bV9Qa8kly74cRvIvtPhHNjp8xxta0c-ku_8_nm42UVG4C39s HTTP/1.1\" 200 2479 -/- (-%) \"http://ad.doubleclick.net/N3847/adi/ebay.uk.x.mmp;sz=728x90;ord=1434020649?\" \"Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)\" \"1.1 CWS_proxy\" \"195.212.10.34\" 0")
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