Regex for date determination in Apache access log
I am writing a python script to fetch data from our Apache Apache 2 access log. Here's one line from the log.
81.52.143.15 - - [01/Apr/2008:00:07:20 -0600] "GET /robots.txt HTTP/1.1" 200 29 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) VoilaBot BETA 1.2 (http://www.voila.com/)"
I am trying to get a part of a date from this string and the regex won't let me, and I'm not sure why. Here's my python code:
l = 81.52.143.15 - - [01/Apr/2008:00:07:20 -0600] "GET /robots.txt HTTP/1.1" 200 29 "-" "Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) VoilaBot BETA 1.2 (http://www.voila.com/)"
re.match(r"\d{2}/\w{3}/\d{4}", l)
returns nothing. Also, do not do the following:
re.match(r"\d{2}/", l)
re.match(r"\w{3}", l)
or anything else I can, even get the date part. What? I do not understand?
a source to share
match () searches for a match at the beginning of a string. Use search () to find a match anywhere in the string. More details here: http://docs.python.org/library/re.html#matching-vs-searching
a source to share
match()
tries to match the entire string. Try it instead search()
.
See also the POWON Regular Expression HOWTO and the Python page for always excellent regular-expressions.info .
a source to share
Instead of using regular expressions to get the date, it might be easier to just split the string into spaces and extract the date:
l = '81.52.143.15 - - [01/Apr/2008:00:07:20 -0600] "GET /robots.txt HTTP/1.1" 200 29 "-" Mozilla/5.0 (Windows; U; Windows NT 5.1; fr; rv:1.8.1) VoilaBot BETA 1.2 (http://www.voila.com/)"'
date = l.split()[3]
If you are processing very large files, this is probably more efficient than using regular expressions.
a source to share