Link to home
Start Free TrialLog in
Avatar of hdau
hdau

asked on

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
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

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

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

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+/);





ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of hdau
hdau

ASKER

Zvonko,
works like a charm!

Thanks,
hdau