Parsing an Apache logfile. Yes, I know that there is such a thing as Apache::ParseLog but since it's OO, I have efficiency worries and anyhow, the real point of this question is for me to hone some regex skills if possible. The problem: my current method looks like this:

while (<LOGFILE>) { my $hit = parse_line($_); #do a bunch of stuff with the hashref, like # insert it into a DB. } sub parse_line { my $line = shift; if ($line =~ /^(\S+).*?\[(\S+).*?] (\S+) "([^"]+)" (\d+)/) { return {host_ip=>$1, timestamp=>$2, vhost=>$3, request=>$4, HTTP_CODE=>$5 }; # and some stuff to handle errors, that needn't bother us }
The problem is that (l)users are sometimes entering something silly into their location windows, such as
"http://www.foo.edu"
(that's right, quotes and all; which, as you can see, messes with my regex, specifically, the parens associated with the fourth and fifth match variables.)

Now I *can* throw them away, and given that these files are taking hours to process (the DB inserts take a while), I'd like to know whether it's worth my while to attempt to handle the silly URLS or whether I should just forget about them as this sub processes over 2 million lines a run and I'd like to make it as lean as possible. How expensive in terms of processing time would it be to craft a smarter regex?

yes, I will deal with that dot-star in there ...

Sample goofy line from log 1.2.3.4 - - [10/Oct/2000:00:19:13 -0400] www.foo.edu "GET /"http://www.TheCounter.com/" HTTP/1.1" 404 2925 i.e. ipaddress, username, realm, timestamp, virtual host name, "request string", http code

Philosophy can be made out of anything -- or less


In reply to Crafting a regex by arturo

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.