#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    Regex from log file


    hey folks,
    sorry for this simple question but i am very bad at regex. i wanted to include a code in my source code to extract date from a log file which contains only this line

    {"sumSt":"10481","lastUpdated":"3\/7\/2012, 4:58 am"}

    i want to extract just 2/6/2013 and nothing else.
    can anyone help me with this?
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Why do people love regexes so much?

    That expression is obviously JSON, so the first step is to parse it with a JSON parser. When that's done, you can access the timestamp with the key "lastUpdated".

    Now you can use a regex to extract the date part. The date is 1 or 2 digits followed by a slash (and a backslash?) followed by 1 or 2 digits followed by 4 digits:

    Code:
    '#\\d{1,2}/\\d{1,2}/\\d{4}#'
    However, I have no idea where you got the "2/6/2013" from. All I see is "3\/7\/2012".

    Comments on this post

    • Nilpo agrees
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0
    Originally Posted by Jacques1
    Why do people love regexes so much?

    That expression is obviously JSON, so the first step is to parse it with a JSON parser. When that's done, you can access the timestamp with the key "lastUpdated".

    Now you can use a regex to extract the date part. The date is 1 or 2 digits followed by a slash (and a backslash?) followed by 1 or 2 digits followed by 4 digits:

    Code:
    '#\\d{1,2}/\\d{1,2}/\\d{4}#'
    However, I have no idea where you got the "2/6/2013" from. All I see is "3\/7\/2012".
    LOL. i was just giving an example what i want to extract. yes it is followed by a back slash which i need to remove. Thanks Jacques1 foryour quick reply.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    You should also find out where the backslash comes from. That looks like a bug (maybe messed up escaping).
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2013
    Posts
    19
    Rep Power
    0

    i used json decode function instead of regex.


    Thanks a lot jacques1 for letting me know about the json format. here is the code i used to extract it

    PHP Code:
    <?
    $json 
    file(DOC_ROOT."/../logs/getMLSTIsolates.log");
    $decoded json_decode($json[0],TRUE);
    list(
    $date$time) = explode(", "$decoded['lastUpdated']);
    ?>
    Last edited by Nilpo; December 3rd, 2013 at 02:27 PM.

IMN logo majestic logo threadwatch logo seochat tools logo