2

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?

2
  • Seems like a weird behaviour. Never tried what you've done above but I always thought it started from the left hand side with the numbering. Commented Nov 25, 2010 at 9:27
  • @mastoj: Yeah.... I thought it might too...which is why I had to investigate! Commented Nov 25, 2010 at 9:34

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

@Mark: You should be careful with this if moving to other programming languages’ regexes. It’s more common for the numbered groups to be strictly left-to-right, whether they also have a name or not.

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.