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");
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".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With