# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r'^(?P<ip>\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}) - - \[(?P<date>.*)\] "(?P<request>.*?)" (?P<status>\d*) (?P<size>\d*) "(?P<referer>.*?)" "(?P<user_agent>.*?)".*$'
test_str = ("192.0.102.40 - - [09/Apr/2018:06:26:33 -0400] \"HEAD /blog HTTP/1.1\" 301 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"192.0.102.40 - - [09/Apr/2018:06:26:33 -0400] \"HEAD /blog HTTP/1.1\" 301 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"198.199.64.224 - - [09/Apr/2018:06:26:33 -0400] \"POST /blog/wp-cron.php?doing_wp_cron=1523269593.5797910690307617187500 HTTP/1.1\" 499 0 \"-\" \"WordPress/4.9.5; https://ivoah.net/blog\"\n"
"192.0.102.40 - - [09/Apr/2018:06:26:33 -0400] \"HEAD /blog/ HTTP/1.1\" 200 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"192.0.102.40 - - [09/Apr/2018:06:31:32 -0400] \"HEAD /blog HTTP/1.1\" 301 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"192.0.102.40 - - [09/Apr/2018:06:31:32 -0400] \"HEAD /blog HTTP/1.1\" 301 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"198.199.64.224 - - [09/Apr/2018:06:31:33 -0400] \"POST /blog/wp-cron.php?doing_wp_cron=1523269893.1204290390014648437500 HTTP/1.1\" 499 0 \"-\" \"WordPress/4.9.5; https://ivoah.net/blog\"\n"
"192.0.102.40 - - [09/Apr/2018:06:31:33 -0400] \"HEAD /blog/ HTTP/1.1\" 200 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"192.0.102.40 - - [09/Apr/2018:06:36:39 -0400] \"HEAD /blog HTTP/1.1\" 301 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"192.0.102.40 - - [09/Apr/2018:06:36:39 -0400] \"HEAD /blog HTTP/1.1\" 301 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"198.199.64.224 - - [09/Apr/2018:06:36:39 -0400] \"POST /blog/wp-cron.php?doing_wp_cron=1523270199.4308791160583496093750 HTTP/1.1\" 499 0 \"-\" \"WordPress/4.9.5; https://ivoah.net/blog\"\n"
"192.0.102.40 - - [09/Apr/2018:06:36:39 -0400] \"HEAD /blog/ HTTP/1.1\" 200 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"47.52.68.60 - - [09/Apr/2018:06:37:08 -0400] \"POST /blog/2016/05/13/mate-on-raspberry-pi/?share=email&nb=1 HTTP/1.1\" 302 5 \"https://ivoah.net/blog/2016/05/13/mate-on-raspberry-pi/?share=email&nb=1\" \"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2)\"\n"
"47.52.68.60 - - [09/Apr/2018:06:37:09 -0400] \"GET /blog/2016/05/13/mate-on-raspberry-pi/?shared=email HTTP/1.1\" 200 30602 \"https://ivoah.net/blog/2016/05/13/mate-on-raspberry-pi/?share=email&nb=1\" \"Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2)\"\n"
"94.23.33.67 - - [09/Apr/2018:06:37:43 -0400] \"GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0\" 301 184 \"-\" \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36\"\n"
"94.23.33.67 - - [09/Apr/2018:06:37:43 -0400] \"GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0\" 301 184 \"-\" \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36\"\n"
"94.23.33.67 - - [09/Apr/2018:06:37:44 -0400] \"GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0\" 404 570 \"-\" \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36\"\n"
"94.23.33.67 - - [09/Apr/2018:06:37:44 -0400] \"GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0\" 301 184 \"-\" \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36\"\n"
"94.23.33.67 - - [09/Apr/2018:06:37:45 -0400] \"GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0\" 301 184 \"-\" \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36\"\n"
"94.23.33.67 - - [09/Apr/2018:06:37:45 -0400] \"GET //wp-content/plugins/cherry-plugin/admin/css/cherry-admin-plugin.css HTTP/1.0\" 404 570 \"-\" \"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36\"\n"
"192.0.102.40 - - [09/Apr/2018:06:41:44 -0400] \"HEAD /blog HTTP/1.1\" 301 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"192.0.102.40 - - [09/Apr/2018:06:41:44 -0400] \"HEAD /blog HTTP/1.1\" 301 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"198.199.64.224 - - [09/Apr/2018:06:41:44 -0400] \"POST /blog/wp-cron.php?doing_wp_cron=1523270504.8351509571075439453125 HTTP/1.1\" 499 0 \"-\" \"WordPress/4.9.5; https://ivoah.net/blog\"\n"
"192.0.102.40 - - [09/Apr/2018:06:41:44 -0400] \"HEAD /blog/ HTTP/1.1\" 200 0 \"-\" \"jetmon/1.0 (Jetpack Site Uptime Monitor by WordPress.com)\"\n"
"207.244.149.74 - - [09/Apr/2018:06:45:38 -0400] \"GET /wp-content/plugins/wp-formgenerator/assets/js/simpleslider.js HTTP/1.1\" 404 341 \"https://ivoah.net/wp-content/plugins/wp-formgenerator/assets/js/simpleslider.js\" \"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.152 Safari/537.36\"\n"
"54.36.148.188 - - [09/Apr/2018:06:45:44 -0400] \"GET /wp/audio/05042011.m4a HTTP/1.1\" 301 184 \"-\" \"Mozilla/5.0 (compatible; AhrefsBot/5.2; +http://ahrefs.com/robot/)\"\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