>>>>
1. Why does regex.GetGroupNumbers() have a Length of 2? I have only one group, (.*?), in my regular expression.
2. Why does Groups 0 and 1 have the values
Password=topsecret and
topsecret as shown?
3.My aim is to extract the Password value (so that I can mask it out with ****). Does it mean that if there is a match, I can safely look for the Password value in Match.Groups[1].ToString() or Match.Groups[1].Captures[0].Value?
4. I am lost. What is Group and what is Capture? Perhaps this should be question 1.
<<<<
1. There is always one Group, Groups[0] that matches the entire match. All other groups are numbered starting from one.
2. Password=topsecret is the entire match. topsecret is the explicit group match.
3. Yes, given your pattern, if you have a match, then you have a Groups[1] that you can work with.Use Groups[1].Value, .Index, and .Length for string position and length. You do not need Captures because your pattern will only capture ONE password.
4. Let me add an example. If you had the pattern @"(?<many>[abc])+" and the string @"123abc456", you will have a match "abc", composed of 3 captures. The match.Groups[1].Value will be "c", i.e, the last capture of the group. But match.Groups[1].Captures[0].Value will be "a". I've named the group "many" so that I can use match.Groups["many"] instead of match.Groups[1].
Les Potter, Xalnix Corporation,
Yet Another C# Blog