Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister

Help with Regex for Apache Error Logs

by coolmichael (Deacon)
on Jun 18, 2002 at 06:47 UTC ( #175293=perlquestion: print w/replies, xml ) Need Help??
coolmichael has asked for the wisdom of the Perl Monks concerning the following question:

In the standard apache error log file format, we have:
[Mon Jun 17 23:09:46 2002] [error]usage: tie @array, Tie::File, filena +me, [option => value]... at d:/httpd/apache/perl/ line 16 [Mon Jun 17 23:24:44 2002] [error] [client]File does not exi +st: d:/httpd/apache/htdocs/kjh
I would like to add a <BR> tag after the blocks of [ ] but before the error begins, like this:
[Mon Jun 17 23:09:46 2002] [error] usage: tie @array, Tie::File, filename, [option => value]... at d:/htt +pd/apache/perl/ line 16 [Mon Jun 17 23:24:44 2002] [error] [client] File does not exist: d:/httpd/apache/htdocs/kjh
My first try was to just go with s/^(\[.*\])/$1<br>/; which worked all too well. . The problem was that it was matching everything up to and including any ]'s in the error string as well. After reading Ovid's writeup(Thank you for Death to Dot Star!), I changed my regex to s/^(\[^]])/$1<br>/;. That change fixed things too much in the other way (only capturing the first [ ]). Now, I've come up with what seems to me to be very complicated.
s/^ #start of string ( #start capture (?: #begin non-capturing repeating \[ #literal [ [^]]* #anything that isn't a ] ]\s* #ending ] and optional spaces )+ #end non capturing repeating ) #end capture / #end match $1<br>/gx;
Iwas surprised at myself for coming up with this. I would very much appreciate any suggestions you might have to simplify this beast. Have I missed anything (I'm very good at missing the good points)?

Thank you.

Replies are listed 'Best First'.
Re: Help with Regex for Apache Error Logs
by greenFox (Vicar) on Jun 18, 2002 at 10:48 UTC

    Your specification seemed to say "match [error] followed by zero or more things contained in square brackets" so I was a bit surprised when your re did not contain the string "error" :) Here is a version that looks for "[error]", not sure if it is any better-

    s/( # begin capture \[error\] # "error" in square brackets :) \s* # optional whitespace (?: # non capture \[ # literal [ [^\]]+ # 1 or more non "]" \] # literal ] )* # end non capture ) # end capture /$1<br>/x;

    my $chainsaw = 'Perl';

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://175293]
Approved by dws
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2017-10-23 22:38 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (285 votes). Check out past polls.