Parsing a syslog using Regex











up vote
-1
down vote

favorite












I am writing a Regex to parse a syslog entry. I am having challenges to able parse the entry until i hit "CMD". I would like all that appears after CMD to be grouped under (). Also, can you please provide suggestions to improve the regex



Here is my syslog entry:




Nov 21 23:17:01 ubuntu-xenial CRON[10299]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)




(<?month>[A-z]{3})s(<?date>[0-9]{2}?)s(<?time>[0-9]+:[0-9]+:[0-9]+)s(<?hostname>[a-z]+-[a-z]+)s(<?daemon>[A-Z]+)(<?pid>[[0-9]+]):s(<?user>([a-z]+))









share|improve this question




















  • 1




    Your regex syntax is completely wrong. This can't work. Your named capture groups are wrong. It should be (<?name>[a-z]+), not [(<?name>)[a-z]+
    – simbabque
    Nov 22 at 17:11










  • "[ ]" were added as i copied from an online parser. but the intent was (<?cpature-group>[a-z]+)
    – Ramesh Rajan
    Nov 22 at 17:25










  • What happens when the day of the month has only a single digit?
    – Tim
    Nov 22 at 19:33















up vote
-1
down vote

favorite












I am writing a Regex to parse a syslog entry. I am having challenges to able parse the entry until i hit "CMD". I would like all that appears after CMD to be grouped under (). Also, can you please provide suggestions to improve the regex



Here is my syslog entry:




Nov 21 23:17:01 ubuntu-xenial CRON[10299]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)




(<?month>[A-z]{3})s(<?date>[0-9]{2}?)s(<?time>[0-9]+:[0-9]+:[0-9]+)s(<?hostname>[a-z]+-[a-z]+)s(<?daemon>[A-Z]+)(<?pid>[[0-9]+]):s(<?user>([a-z]+))









share|improve this question




















  • 1




    Your regex syntax is completely wrong. This can't work. Your named capture groups are wrong. It should be (<?name>[a-z]+), not [(<?name>)[a-z]+
    – simbabque
    Nov 22 at 17:11










  • "[ ]" were added as i copied from an online parser. but the intent was (<?cpature-group>[a-z]+)
    – Ramesh Rajan
    Nov 22 at 17:25










  • What happens when the day of the month has only a single digit?
    – Tim
    Nov 22 at 19:33













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I am writing a Regex to parse a syslog entry. I am having challenges to able parse the entry until i hit "CMD". I would like all that appears after CMD to be grouped under (). Also, can you please provide suggestions to improve the regex



Here is my syslog entry:




Nov 21 23:17:01 ubuntu-xenial CRON[10299]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)




(<?month>[A-z]{3})s(<?date>[0-9]{2}?)s(<?time>[0-9]+:[0-9]+:[0-9]+)s(<?hostname>[a-z]+-[a-z]+)s(<?daemon>[A-Z]+)(<?pid>[[0-9]+]):s(<?user>([a-z]+))









share|improve this question















I am writing a Regex to parse a syslog entry. I am having challenges to able parse the entry until i hit "CMD". I would like all that appears after CMD to be grouped under (). Also, can you please provide suggestions to improve the regex



Here is my syslog entry:




Nov 21 23:17:01 ubuntu-xenial CRON[10299]: (root) CMD ( cd / && run-parts --report /etc/cron.hourly)




(<?month>[A-z]{3})s(<?date>[0-9]{2}?)s(<?time>[0-9]+:[0-9]+:[0-9]+)s(<?hostname>[a-z]+-[a-z]+)s(<?daemon>[A-Z]+)(<?pid>[[0-9]+]):s(<?user>([a-z]+))






regex perl regex-group






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 23 at 5:31









James Z

11.1k71735




11.1k71735










asked Nov 22 at 16:55









Ramesh Rajan

32




32








  • 1




    Your regex syntax is completely wrong. This can't work. Your named capture groups are wrong. It should be (<?name>[a-z]+), not [(<?name>)[a-z]+
    – simbabque
    Nov 22 at 17:11










  • "[ ]" were added as i copied from an online parser. but the intent was (<?cpature-group>[a-z]+)
    – Ramesh Rajan
    Nov 22 at 17:25










  • What happens when the day of the month has only a single digit?
    – Tim
    Nov 22 at 19:33














  • 1




    Your regex syntax is completely wrong. This can't work. Your named capture groups are wrong. It should be (<?name>[a-z]+), not [(<?name>)[a-z]+
    – simbabque
    Nov 22 at 17:11










  • "[ ]" were added as i copied from an online parser. but the intent was (<?cpature-group>[a-z]+)
    – Ramesh Rajan
    Nov 22 at 17:25










  • What happens when the day of the month has only a single digit?
    – Tim
    Nov 22 at 19:33








1




1




Your regex syntax is completely wrong. This can't work. Your named capture groups are wrong. It should be (<?name>[a-z]+), not [(<?name>)[a-z]+
– simbabque
Nov 22 at 17:11




Your regex syntax is completely wrong. This can't work. Your named capture groups are wrong. It should be (<?name>[a-z]+), not [(<?name>)[a-z]+
– simbabque
Nov 22 at 17:11












"[ ]" were added as i copied from an online parser. but the intent was (<?cpature-group>[a-z]+)
– Ramesh Rajan
Nov 22 at 17:25




"[ ]" were added as i copied from an online parser. but the intent was (<?cpature-group>[a-z]+)
– Ramesh Rajan
Nov 22 at 17:25












What happens when the day of the month has only a single digit?
– Tim
Nov 22 at 19:33




What happens when the day of the month has only a single digit?
– Tim
Nov 22 at 19:33












1 Answer
1






active

oldest

votes

















up vote
0
down vote



accepted










Here's my revisions, with comments. In general, you might be better off making fewer assumptions about what the fields are going to contain. Here I'm using S which is "anything except whitespace". Furthermore, s+ will match some whitespace, whether that's just one character or more.



(<?month>S+)                    #
s+ # added + because single digit dates might have additional spaces
(<?date>[0-9]{1,2}) # changed {2}? to {1,2} because you might have one or two digits
s+ #
(<?time>[0-9]+:[0-9]+:[0-9]+) #
s+ #
(<?hostname>S+) # anything which isn't whitespace
s+ #
(<?daemon>S+) # just in case your daemon has a digit or lower case in its name
(<?pid>[[0-9]+]) #
: #
s+ #
((<?user>S+)) # your username might have digits in it; don't capture the brackets
s+ #
CMD #
s+ #
((<?command>.*)) # capture the command, not the brackets
s* # in case of trailing space
$ # match end of string





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435437%2fparsing-a-syslog-using-regex%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote



    accepted










    Here's my revisions, with comments. In general, you might be better off making fewer assumptions about what the fields are going to contain. Here I'm using S which is "anything except whitespace". Furthermore, s+ will match some whitespace, whether that's just one character or more.



    (<?month>S+)                    #
    s+ # added + because single digit dates might have additional spaces
    (<?date>[0-9]{1,2}) # changed {2}? to {1,2} because you might have one or two digits
    s+ #
    (<?time>[0-9]+:[0-9]+:[0-9]+) #
    s+ #
    (<?hostname>S+) # anything which isn't whitespace
    s+ #
    (<?daemon>S+) # just in case your daemon has a digit or lower case in its name
    (<?pid>[[0-9]+]) #
    : #
    s+ #
    ((<?user>S+)) # your username might have digits in it; don't capture the brackets
    s+ #
    CMD #
    s+ #
    ((<?command>.*)) # capture the command, not the brackets
    s* # in case of trailing space
    $ # match end of string





    share|improve this answer



























      up vote
      0
      down vote



      accepted










      Here's my revisions, with comments. In general, you might be better off making fewer assumptions about what the fields are going to contain. Here I'm using S which is "anything except whitespace". Furthermore, s+ will match some whitespace, whether that's just one character or more.



      (<?month>S+)                    #
      s+ # added + because single digit dates might have additional spaces
      (<?date>[0-9]{1,2}) # changed {2}? to {1,2} because you might have one or two digits
      s+ #
      (<?time>[0-9]+:[0-9]+:[0-9]+) #
      s+ #
      (<?hostname>S+) # anything which isn't whitespace
      s+ #
      (<?daemon>S+) # just in case your daemon has a digit or lower case in its name
      (<?pid>[[0-9]+]) #
      : #
      s+ #
      ((<?user>S+)) # your username might have digits in it; don't capture the brackets
      s+ #
      CMD #
      s+ #
      ((<?command>.*)) # capture the command, not the brackets
      s* # in case of trailing space
      $ # match end of string





      share|improve this answer

























        up vote
        0
        down vote



        accepted







        up vote
        0
        down vote



        accepted






        Here's my revisions, with comments. In general, you might be better off making fewer assumptions about what the fields are going to contain. Here I'm using S which is "anything except whitespace". Furthermore, s+ will match some whitespace, whether that's just one character or more.



        (<?month>S+)                    #
        s+ # added + because single digit dates might have additional spaces
        (<?date>[0-9]{1,2}) # changed {2}? to {1,2} because you might have one or two digits
        s+ #
        (<?time>[0-9]+:[0-9]+:[0-9]+) #
        s+ #
        (<?hostname>S+) # anything which isn't whitespace
        s+ #
        (<?daemon>S+) # just in case your daemon has a digit or lower case in its name
        (<?pid>[[0-9]+]) #
        : #
        s+ #
        ((<?user>S+)) # your username might have digits in it; don't capture the brackets
        s+ #
        CMD #
        s+ #
        ((<?command>.*)) # capture the command, not the brackets
        s* # in case of trailing space
        $ # match end of string





        share|improve this answer














        Here's my revisions, with comments. In general, you might be better off making fewer assumptions about what the fields are going to contain. Here I'm using S which is "anything except whitespace". Furthermore, s+ will match some whitespace, whether that's just one character or more.



        (<?month>S+)                    #
        s+ # added + because single digit dates might have additional spaces
        (<?date>[0-9]{1,2}) # changed {2}? to {1,2} because you might have one or two digits
        s+ #
        (<?time>[0-9]+:[0-9]+:[0-9]+) #
        s+ #
        (<?hostname>S+) # anything which isn't whitespace
        s+ #
        (<?daemon>S+) # just in case your daemon has a digit or lower case in its name
        (<?pid>[[0-9]+]) #
        : #
        s+ #
        ((<?user>S+)) # your username might have digits in it; don't capture the brackets
        s+ #
        CMD #
        s+ #
        ((<?command>.*)) # capture the command, not the brackets
        s* # in case of trailing space
        $ # match end of string






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 22 at 19:53

























        answered Nov 22 at 19:47









        Tim

        7,8512344




        7,8512344






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53435437%2fparsing-a-syslog-using-regex%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            What visual should I use to simply compare current year value vs last year in Power BI desktop

            How to ignore python UserWarning in pytest?

            Alexandru Averescu