Re: Can anyone help me with a quick PERL question?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, 8 Oct 2004, James Marcinek wrote:

> First off let me state that I don't want to have to subscribe to a
> list for just one question and I'm assuming that someone out here in
> this technical savy group can help me.

Fair enough.  But the next time you need Perl help head to
http://perlmonks.org.  It's a great place to post Perl questions.

> Here is one line from the log:
> 
> <165>Oct 01 2004 19:57:52: %PIX-5-111008: User 'enable_15' executed the 'logging trap 5' command.

> Below is my REGEX parsing line (I've been changing it to
> try and get it to work:
> 
> if ($raw =~
> /(^<\d\d\d>)(\w\w\w)\s+(\d\d)\s+(\d\d\d\d)\s*(\d\d:\d\d:\d\d):\s+(%\w\w\w-\d+-\d+):\s*(\w*$)
> ) { ...

I believe the problem is at the end of your regex.  You're expecting:

  (\w*$)

To match "User 'enable_15'..." but \w only matches a single set of
letters and digits.  Try this:

  (.*)$

which will match the rest of the line.  

Of course, there's lots of things you could do to make your regex
better and more resilent to changes in the data.  For one thing,
instead of counting out specific numbers of digits and characters, try
just collecting whatever is there:

  /^(<\d+>)(\w+)\s+(\d+)\s+(\d+)\s*(\d+:\d+:\d+):\s+(%\w+-\d+-\d+):\s*(.*)$/

Even better, use the /x modifier and include docs:

  /^(<\d+>) # $1: the log code in angle-brakets
    (\w+)   # $2: some word characters
            # ...
  /x

This will make it easier to find problems.  If you want to learn more
about regexes pick up a copy of Mastering Regular Expressions from
O'Reilly.

-sam



[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux