[syslog-ng] Python help
chris packham
chris.packham at alliedtelesis.co.nz
Wed Sep 16 08:11:58 CEST 2009
On Wed, 2009-09-16 at 11:03 +0530, Jain, Vaibhav (GE Healthcare) wrote:
> Hi,
>
> I am using a python parser for parsing the incoming message from
> syslog-ng. In my log messages some fields are optional and some are
> not.I want to know how to handle these optional parameters in the
> python regual expression.
>
Your question is about python not syslog so you might have more luck
from a python forum/mailing list.
Having said that, the standard way of making a group optional in a regex
is to add ? after it e.g. \d(\w+)? means a single digit followed by and
optional string of word characters.
Here's a small snippet I whipped up based on your example data.
---
import re;
data = ['Sep 15 22:23:37 ecis007 codeSystemName=5 UserID="Admin"',
'Sep 15 22:23:37 ecis007 UserID="Admin"']
r = re.compile (r'(\w+)\s+(\d+)\s+(\d\d:\d\d:\d\d)\s+([^\s]+)\s+(codeSystemName=(\d+)\s+)?(.*)')
for d in data:
print "d="+d
m = r.match(d);
if m:
print repr(m.groups())
---
# python test.py
d=Sep 15 22:23:37 ecis007 codeSystemName=5 UserID="Admin"
('Sep', '15', '22:23:37', 'ecis007', 'codeSystemName=5 ', '5',
'UserID="Admin"')
d=Sep 15 22:23:37 ecis007 UserID="Admin"
('Sep', '15', '22:23:37', 'ecis007', None, None, 'UserID="Admin"')
More information about the syslog-ng
mailing list