mpen
mpen

Reputation: 282885

C# Regexes: Named and numbered capturing groups

Given

var re = new Regex(@"(?<name>\w+)(x)\1\2");
Console.WriteLine(re.IsMatch("yxxy"));

Prints True, it appears unnamed capturing groups are numbered starting with 1, and then named capturing groups are assigned numbers afterwords. Is this correct/intended behaviour?

Upvotes: 2

Views: 1791

Answers (1)

alpha-mouse
alpha-mouse

Reputation: 5003

At least MSDN says so. http://msdn.microsoft.com/en-us/library/bs2twtah(VS.71).aspx. In the newer version of this article it is confirmed, but it's more difficult to find this info. Named matched subexpressions are numbered consecutively from left to right after matched subexpressions.

Upvotes: 3

Related Questions