Regular expression to split log file string according to time stamp format

Experts,
I have to parse server log files in log4j format (using JavaScript) and struggle a bit with the regular expression syntax to extract individual entries.
Every new log entry starts with (example):
      2007-07-02 19:37:48,296 herecomesthelogtext...
which is timestamp information (yyyy-mm-dd hh:mm:ss,ms), in 24hr format
After that timestamp, any number of lines, line feeds, empty lines, characters ... can appear, until the next entry.

What would be the correct (regular expression ?) syntax to parse the log string so that I get the individual log entries into an array (as well as the timestamp information) ?
Currently I use a modified log file with "@@@" as the start of each log entry, read in all of the string and then use string.split("@@@"), but apart from not being elegant, I might not be able to modify that logging system any longer in the future and have to rely on the more generic option.

Thanks in advance,
hdau
hdauAsked:
Who is Participating?
 
ZvonkoSystems architectCommented:
If you want to keep the time stamps together with the records, then you need this:

("\n"+string).split(/[\n\r]\s*(?=\d{4}-\d\d\-\d\d (\d\d[\:\,]){3}\d+\s+)/g)


0
 
ZvonkoSystems architectCommented:
Instead of string.split("@@@") use this:

("\n"+string).split(/[\n\r]\d{4}-\d\d\-\d\d (\d\d[\:\,]){4}\d+\s+/);

0
 
ZvonkoSystems architectCommented:
Sorry, not four but three digit pairs:

("\n"+string).split(/[\n\r]\d{4}-\d\d\-\d\d (\d\d[\:\,]){3}\d+\s+/);

Or write it down:

("\n"+string).split(/[\n\r]\d\d\d\d-\d\d\-\d\d \d\d\:\d\d\:\d\d\,\d+\s+/);





0
 
hdauAuthor Commented:
Zvonko,
works like a charm!

Thanks,
hdau
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.