in reply to Variable Length Parsing

Mostly, you are splitting up your line by whitespace, and split is a good tool for that job. But ... this variable length field you speak of throws a monkey wrench in the gears. Here is a regex that should do the trick:
use strict; use warnings; use Data::Dumper; my $str = 'May 16 11:17:12 system app: [id:id:id] Message [Class] [Pri +ority:] {TCP} 111.1.1.64:1863 -> 222.2.2.155:55527'; # no need to use $1 $2 etc. when you can name them ;) my ($mon,$day,$time,$app,$id,$msg,$proto,@ip) = $str =~ / # example match (\S+)\s+ # May (\S+)\s+ # 16 (\S+)\s+ # 11:17:12 ([^:]+):\s+ # system app (\S+)\s+ # [id:id:id] ([^\{]+) # Message [Class] [Priority] (\{[^\}]+\})\s+ # {TCP} (\S+) # 111.1.1.64:1863 \s+\-\>\s+ # (skip ->) (\S+) # 222.2.2.155:55527 /x; print "$mon, $day, $time, $app, $id, $msg, $proto, @ip\n";
The important concept to take away from this is negation: (\S+) matches anything but whitespace, ([^:]+) matches anything up to a colon. I stored the two IP addresses in an array, your mileage will vary. Also, (update here) any complex regex such as this one should use the x modifier so it can be segmented and commented.

jeffa

L-LL-L--L-LL-L--L-LL-L--
-R--R-RR-R--R-RR-R--R-RR
B--B--B--B--B--B--B--B--
H---H---H---H---H---H---
(the triplet paradiddle with high-hat)

Replies are listed 'Best First'.
Re: (jeffa) Re: Variable Length Parsing
by ACiD (Novice) on Jun 08, 2003 at 21:51 UTC
    All, Thank you kindly. I tried several of your tactics and got it to break off at the {TCP} (still have more work though. I did not know you could use ^blah in mid stream. I thought it was "beginning of line" (opposite $). I assume that since I () paren'd it, then it WAS the beginning of the next undefined $4 (or whatever number) ?? (again forgive the ignorance with respect to the code and language.)
      Yes, ^ can be used to negate a character class as well as anchor a match - it was not the parens that changed context, it was the square brackets (which denote a character class). Check out the docs, in particular, the sentence below xdigit.

      jeffa

      L-LL-L--L-LL-L--L-LL-L--
      -R--R-RR-R--R-RR-R--R-RR
      B--B--B--B--B--B--B--B--
      H---H---H---H---H---H---
      (the triplet paradiddle with high-hat)