Greetings. I am a PERL programmer (there, I've said it) who can't fight his way out of a regular-expression-wet-paper-bag. It's not that I don't understand them, they are incredibly powerful and due to my time constraints seem to be hiding from me. So I thought I would ask here.
Currently, I write code that processes text messages sent from network devices (typically SNMP traps). Recently our dear friend Cisco upgraded their VPN concentrator IOS and "fixed" a bunch of messages they had previously left inconsistent (similar messages had slight syntactical variations).
My program originally parsed the message using split in this fashion:
($KEY, $DATE, $TIMESTAMP, $SEV, $LOG_NUM, $RPT, $IP_ADDR, $H1, $H2, + $USER, $H3, $H4, $H5, $H6, $H7, $H8, $H9, $H10, $H11, $H12, $H13, $D +URATION) = split(/ /, $VAR2);
Now I find myself stuck because my parsing routine isn't nearly smart enough to handle the new message formats. Basically, I am looking for a regular expression technique to pick out the elements I need from the message without having to split the string in pieces (because the number of pieces now fluctuates depending on the message). Here are two sample messages (an old one and a new one)
Here is the new message, notice slight deviations[2] private.enterprises.3076.2.1.4.4.15.22 (OctetString): 65479332 04/ +07/2004 04:15:21.980 SEV=4 AUTH/22 RPT=8622 User silk01 connected
[2] private.enterprises.3076.2.1.4.4.15.22 (OctetString): 1064993 04/1 +2/2004 01:02:31.890 SEV=4 AUTH/22 RPT=362 User [schk01] Group [cisco3015] connected, Session Type: IPSec
My parsing isn't flexible enough to handle the shifting data and the code administration for my current routine is ugly. Can someone suggest an alternative using regular expressions that would parse out the fields I need. In the example above, I need the user ID, the group, the status of connected and the session type. I can detect the presence of a new version message vs. an old message so I can programmatically handle them differently - I don't need a regex that handles both.
Thanks in advance, I'm a little over my head here.
20040414 Edit by BazB: Changed title from 'Regular Expressions Hate Me'
In reply to Regex for IOS messages by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |