Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regexes (.NET dialect): strange behaviour of a capturing group

Tags:

.net

regex

I'm stuck. Why does the group path in this code have value 2/3/4, not 1/2/3/4? Where did 1/ go? What part of the expression matches 1/?

var re = new Regex(@"^-/?(?'folder'((?'path'.+?)/)??[^/]*)/?$");
var m = re.Match("-1/2/3/4/5");
m.Groups["folder"].Value.Dump("Folder");
m.Groups["path"].Value.Dump("Path");
like image 473
thorn0 Avatar asked Jan 24 '11 14:01

thorn0


1 Answers

It looks like this is a behavioural difference between .NET 3.5 and 4.0. Here's a complete program:

using System;
using System.Text.RegularExpressions;

class Test
{
    static void Main()
    {
        var re = new Regex(@"^-/?(?'folder'((?'path'.+?)/)??[^/]*)/?$");
        var m = re.Match("-1/2/3/4/5");
        Console.WriteLine("Folder: " + m.Groups["folder"].Value);
        Console.WriteLine("Path: " + m.Groups["path"].Value);
    }
}

Compiled and run under .NET 3.5:

Folder: 1/2/3/4/5
Path: 2/3/4

Compiled and run under .NET 4:

Folder: 1/2/3/4/5
Path: 1/2/3/4

I don't know why the behaviour should vary though...

EDIT: I've investigated this a bit further... under .NET 3.5, the group consists of two captures: "1" and "2/3/4". Under .NET 4 it's the single capture "1/2/3/4".

like image 73
Jon Skeet Avatar answered Sep 22 '22 04:09

Jon Skeet