Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Help with Regex for Apache Error Logs

by coolmichael (Deacon)
on Jun 18, 2002 at 06:47 UTC ( [id://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/velog.pl line 16 [Mon Jun 17 23:24:44 2002] [error] [client 127.0.0.1]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/velog.pl line 16 [Mon Jun 17 23:24:44 2002] [error] [client 127.0.0.1] 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?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://175293]
Approved by dws
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-24 23:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found