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

Posted on 2007-07-25
Last Modified: 2012-06-21
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,
Question by:hdau
    LVL 63

    Expert Comment

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

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

    LVL 63

    Expert Comment

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

    LVL 63

    Accepted Solution

    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)


    Author Comment

    works like a charm!


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
    Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    7 Experts available now in Live!

    Get 1:1 Help Now