Friday, 12 January 2007

More on Regular Expression patterns

Regular expression patterns are powerful. I have been recently using Log4Net to log system activity and created a regular expression similar to the one below to allow multi-line text log parsing.

Sample Text:

2007-01-01 00:00:00 Test log message
Another line
Yet one more line
2002-01-01 00:01:01 Another test log message

Regular Expression Pattern:

^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})[ ]*((?:(?!\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*(?:(?!\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}).*\n?)*))

The above regular expression pattern will capture Date and parse the LogText where the additional lines don't begin with the Date.

No comments: