Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1027
  • Last Modified:

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
0
hdau
Asked:
hdau
  • 3
1 Solution
 
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
 
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
 
hdauAuthor Commented:
Zvonko,
works like a charm!

Thanks,
hdau
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now