title: Apache access log regex parsing
body: <p>I have a custom access LOG for Apache:</p>
  
  <pre><code>LogFormat "%h %l %u %t \"%r\" %&gt;s %b \"%{Referer}i\" \"%{User-Agent}i\" %{JSESSIONID}C %D %V" mylog
  </code></pre>
  
  <p>I am trying to parse from Python the LOGs generated; but I have two problems:</p>
  
  <ul>
  <li>Requests without request method (HTTP/1.0 or HTTP/1.1) are not parsed correctly.</li>
  <li>Request with spaces in the requested path are not parsed correctly (I don't know if Apache saves this path encoded or keeps the spaces, but I could generate a LOG line making a request by hand in telnet).</li>
  </ul>
  
  <p>Using this regex:</p>
  
  <pre><code>(?P&lt;ip&gt;.*) (?P&lt;remote_log_name&gt;.*) (?P&lt;userid&gt;.*) \[(?P&lt;date&gt;.*)(?= ) (?P&lt;timezone&gt;.*?)\] \"(?P&lt;request_method&gt;.*) (?P&lt;path&gt;.*)(?P&lt;request_version&gt; HTTP/.*)\" (?P&lt;status&gt;.*) (?P&lt;length&gt;.*) \"(?P&lt;referrer&gt;.*)\" \"(?P&lt;user_agent&gt;.*)\" (?P&lt;session_id&gt;.*) (?P&lt;generation_time_micro&gt;.*) (?P&lt;virtual_host&gt;.*)
  </code></pre>
  
  <p>The parsing fails with the first 3 lines of this LOG:</p>
  
  <pre><code>1.1.1.2 - - [11/Nov/2016:03:04:55 +0100] "GET /" 200 83 "-" "-" - 9221 1.1.1.1
  127.0.0.1 - - [11/Nov/2016:14:24:21 +0100] "GET /uno dos" 404 298 "-" "-" - 400233 1.1.1.1
  127.0.0.1 - - [11/Nov/2016:14:23:37 +0100] "GET /uno dos HTTP/1.0" 404 298 "-" "-" - 385111 1.1.1.1
  1.1.1.1 - - [11/Nov/2016:00:00:11 +0100] "GET /icc HTTP/1.1" 302 - "-" "XXX XXX XXX" - 6160 11.1.1.1
  1.1.1.1 - - [11/Nov/2016:00:00:11 +0100] "GET /icc/ HTTP/1.1" 302 - "-" "XXX XXX XXX" - 2981 1.1.1.1
  </code></pre>
  
  <p>Regex can be simulated here <a href="https://regex101.com/r/xDfSqj/2" rel="nofollow noreferrer">https://regex101.com/r/xDfSqj/2</a>.</p>