开发者

Regex named group problem C#

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?


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")
)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜