0

Purpose: Trusted user puts up a crontab line, give advice when then syntax is wrong

I got this one which seems to work well:

/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+\S+\s+.+\S*$/

I want to improve (learning purpose) and tried to reduce the \S+\s:

/^\s*[(\S+)\s+]{6}.+\S*$/

When you look at typical crontab lines, all its values are seperated by (any amount of) 6 whitespace characters.

   2-20/2    10,20     /3         * *         user very -long -cryptic > comand
^\s* \S+  \s+  \S+  \s+ \S+ \s+ \S+\s+\S+\s+    \S*                             $

What's wrong with the short pattern?

Daniel W.
  • 31,164
  • 13
  • 93
  • 151
  • `[]` denotes a character class. You want a group `()` – HamZa Jul 02 '14 at 21:52
  • @HamZa how can I tell it to repeat the pattern? I also tried `((\S+)\s+){6}` and `(\S+\s+){6}` which didn't work. – Daniel W. Jul 02 '14 at 21:59
  • `[(\S+)\s+]` is a character class which will match `()+\s\S`. Basically everything. You must use a group `((\S+)\s+)`. Could you explain what doesn't work in [this demo](http://regex101.com/r/aT8sU6/1) ? – HamZa Jul 02 '14 at 22:01
  • The only whitespace characters you're dealing with are simple spaces (` `), correct? – CAustin Jul 02 '14 at 22:03
  • @HamZa it shows 1 match: `user`, but I need the timings (first 5), see [this demo](http://www.phpliveregex.com/p/5Re). @CAustin it can be several whitespaces or `\t`, basically, any whitespace character. – Daniel W. Jul 02 '14 at 22:08
  • 1
    @DanFromGermany [Read this thread](http://stackoverflow.com/questions/15268504/collapse-and-capture-a-repeating-pattern-in-a-single-regex-expression), quite similar. Basically you can't get all matches from a repeated group like that. You will need to use a slightly different method. Something like [this](http://regex101.com/r/pE2uG1/1) `\G\s*\K\S+` with `preg_match_all()`. On that note, I think it's simpler to use `preg_split()` with a simple pattern like `\s+` – HamZa Jul 02 '14 at 22:12
  • Specifying "any whitespace character" with `\s` can be kind of dangerous, since that also includes carriage returns and line feeds. Anyway, do you actually need to return the values of your capture groups, or do you simply need to assert a match? If you don't need the capture groups, then what you're trying to do would be possible. Otherwise, the link posted by HamZa addresses the problem here. – CAustin Jul 02 '14 at 22:20

0 Answers0