C# Regex Question

J

Jason Newell

All,

I'm needing a little help parsing a log file using Regex. Here is an
example line from the log.

[2010-05-21 02:42:15,307] [WARN,CommandHandler] Duplicate entry

I want to simply break the line into the following chunks:

[2010-05-21 02:42:15,307]
[WARN,CommandHandler]
Duplicate entry

I've tried the following Regex is some success:
\[[^\]]*\]
(?<DateTime>\[[^\]]*\]) (?<Message>.*)

I've already spent quite a bit of time on my own trying to figure it
out. Thanks for any help.

Jason Newell
www.jasonnewell.net
 
J

Jackie

All,

I'm needing a little help parsing a log file using Regex. Here is an
example line from the log.

[2010-05-21 02:42:15,307] [WARN,CommandHandler] Duplicate entry

I want to simply break the line into the following chunks:

[2010-05-21 02:42:15,307]
[WARN,CommandHandler]
Duplicate entry

I've tried the following Regex is some success:
\[[^\]]*\]
(?<DateTime>\[[^\]]*\]) (?<Message>.*)

I've already spent quite a bit of time on my own trying to figure it
out. Thanks for any help.

Jason Newell
www.jasonnewell.net

I am not exactly sure what the problem is. The regular expression seems
to work fine, just that DateTime isn't at the right place. If you want
to replace the text with regex to break those onto their own lines,
can't you just replace it all with
${DateTime}\n${WhatThisIs}\n${Message}
?
And you need to use proper line breaks, of course (\n, \r or \r\n).
:)
 
J

Jackie

After reading your post again, it seems like what you want to do is this:

^(?<DateTime>\[[^\]]*\]) (?<PleaseNameIt>\[[^\]]*\]) (?<Message>.*)$

I didn't really change much and the result is this:

DateTime: [2010-05-21 02:42:15,307]
PleaseNameIt: [WARN,CommandHandler]
Message: Duplicate entry
 
J

Jackie

Jackie,

That one did the trick. Thank you soo much!

Jason Newell
www.jasonnewell.net
After reading your post again, it seems like what you want to do is this:

^(?<DateTime>\[[^\]]*\]) (?<PleaseNameIt>\[[^\]]*\]) (?<Message>.*)$

I didn't really change much and the result is this:

DateTime: [2010-05-21 02:42:15,307]
PleaseNameIt: [WARN,CommandHandler]
Message: Duplicate entry

I didn't really do much but you're welcome. :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top