-1

I want to extract the cron expression from a crontab line. I tried 2 regular expressions (pcre php):

^(.*?)(?=[a-zA-Z])

.. which works for */1 * * * * wget --spider ...

and

^(.*?)(?=\/[a-zA-Z])

.. which works for */1 * * * * /my/path/ ...

I also tried to combine both:

^(.*?)(?=\/[a-zA-Z])|(?=[a-zA-Z])

... but this catches the "wget" in sample 1, too. I dont want to match the exact expression */1 * * * *, this is just a sample. It must work for any cron expression.

Benni
  • 1,023
  • 11
  • 15
  • 1
    Searching for "Regex match cron expression" returns so many results that it isn't even funny anymore. – Tomalak Mar 02 '18 at 15:33
  • Possible duplicate of [how to create a regular expression for cron](https://stackoverflow.com/questions/14203122/how-to-create-a-regular-expression-for-cron) – Bob Dalgleish Mar 02 '18 at 15:40
  • The answers in the linked possible duplicate all don't seem to work for me, but I will take a closer look – Benni Mar 02 '18 at 15:53
  • @Tomalak: I totally disagree that your proposed search term, neither on stackoverflow nor on google, returns a lot of good results. – Benni Mar 02 '18 at 23:26

1 Answers1

-1

After all I ended up with this imperfect but working solution:

/((?<=^|\s)\*\/?\d*|(?<=^|\s)\d+(?=\s)|(?<=^|\s)\d+(?:\,\d+)+)/g

I know it does not cover the complete crontab expression syntax. It will also match fragments that come later in the entry line, but I think usually there won't be any detached digits/asterisks. Correct me if I'm wrong.

Benni
  • 1,023
  • 11
  • 15