[Wellington-pm] lookahead/behind in regex

michael at diaspora.gen.nz michael at diaspora.gen.nz
Mon Oct 11 22:41:30 CDT 2004


Richard Hector writes:
>perl -p -e 's/^(?<=\[)(\d+)(?=\])/localtime($1)/e' < nagios.log

This does, though:

    perl -p -e 's/(?<=^\[)(\d+)(?=\])/localtime($1)/e' < nagios.log

Although I can't explain why in words.

If I was looking at that code later, though, I'd wonder why you didn't
just do:

    perl -pe 's/(\d+)/localtime $1/e' < nagios.log

(or even s/\d+/localtime $&/e, just to terrify Grant :)

Which also works, even in the face of extra digit fields later on.
Of course, I'm assuming that all lines start with \[\d+\], and since
I don't have a real Nagios log to work on at present, that's a big
assumption.
    -- michael.


More information about the Wellington-pm mailing list