Odd regexp behaviour - matches only first and last capture group

The value given by match.Groups[2].Value is just the last value captured by the second group.

To find all the values, look at match.Groups[2].Captures[i].Value where in this case i ranges from 0 to 2. (As well as match.Groups[1].Value for the first group.)

(+1 for question, I learned something today!)


Try this:

string text = "   apple , banana ,orange,peanut";

var matches = Regex.Matches(text, @"\s*(?<word>\w+)\s*,?")
        .Cast<Match>()
        .Select(x => x.Groups["word"].Value)
        .ToList();

You are repeating your capturing group, at every repeated match the previous content is overwritten. So only the last match of your second capturing group is available at the end.

You can change your second capturing group to

^\s*([a-z_]\w*)((?:\s*,\s*(?:[a-z_]\w*))*)\s*$

Then the result would be " , banana ,orange,peanut" in your second group. I am not sure, if you want this.

If you want to check that the string has that pattern and extract each word. I would do it in two steps.

  1. Check the pattern with your regex.

  2. If the pattern is correct, remove leading and trailing whitespace and split on \s*,\s*.