0

I'm trying to write a regex with named captures in C# to parse cron jobs. The problem, however, is that both using single quote and bracket named capture groups the values return blank. For example, with the input

*/15 * * * * * http://www.google.com/

this code:

Regex cronRe = new Regex(@"(?<minutes>[\d\*\-,]+) (?<hours>[\d\*\-,]+) (?<days>[\d\*\-,]+) (?<months>[\d\*\-,]+) (?<dotw>[\d\*\-,]+) (?<years>[\d\*\-,]+) (?<command>[\d\*\-,]+)");

//loop through jobs and do them
for (int i = 0; i < lines.Length; i++)
{
    Match line = logRe.Match(lines[i]);
    bool runJob = true;
    for (int j = 0; j < line.Groups.Count; j++)
    {
        Console.Write(j.ToString() + ": " + line.Groups[j].Value + "\n");
    }
    Console.Write("named group minutes: " + line.Groups["minutes"].Value);
}

return with this:

0: */15 * * * * * http://www.google.com
1: */15 *
2: * 3: *
4: *
5: *
6: http://www.google.com
named group minutes:

any suggestions?

2
  • What would you like it to return? Sorry not familiar with cron jobs... Could you post a couple more examples of what you would be parsing? Commented Oct 9, 2009 at 15:20
  • In your example, you have defined CronRe but in the loop you're using LogRe.. is that a typo in the example or are you using a different regex there? Commented Oct 9, 2009 at 15:27

1 Answer 1

5

I believe you want something like this (line-wrapped for readability):

^
(?<minutes>[\d*,/-]+)\s
(?<hours>[\d*,/-]+)\s
(?<days>[\d*,/-]+)\s
(?<months>[\d*,/-]+)\s
(?<dotw>[\d*,/-]+)\s
(?<years>[\d*,/-]+)\s
(?<command>.*)
$

Notes:

  • The expression must be properly anchored (at least "^").
  • The dash has special meaning in character classes (it creates ranges). If you want to match a literal dash, put it at the end of the character class.
  • You don't need to escape the star within a character classes.

Apart from that: The expression ([\d*,/-]+) is fairly unspecific. I'd do it with more input validation:

(\d+(?:-\d+)?(?:,\d+(?:-\d+)?)*(?:,\*)?|\*(?:/\d+)?)

Explanation

(
  \d+                 // matches numbers ("3")
  (?:-\d+)?           // with the above: matches ranges ("3-4")
  (?:                 // optional
    ,\d+              // matches more numbers ("3,6")
    (?:-\d+)?         // matches more ranges ("3,6-9")
  )*                  // allows repeat ("3,6-9,11")
  (?:,\*)?            // allows star at the end ("3,6-9,11,*")
  |                   // alternatively...
  \*(?:/\d+)?         // allows star with optional filter ("*" or "*/15")
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.