I am trying to write a regexp which would match a comma separated list of words and capture all words. This line should be matched apple , banana ,orange,peanut and captures should be apple, banana, orange, peanut. To do that I use following regexp:
^\s*([a-z_]\w*)(?:\s*,\s*([a-z_]\w*))*\s*$
It successfully matches the string but all of a sudden only apple and peanut are captured. This behaviour is seen in both C# and Perl. Thus I assume I am missing something about how regexp matching works. Any ideas? 🙂
The value given by
match.Groups[2].Valueis just the last value captured by the second group.To find all the values, look at
match.Groups[2].Captures[i].Valuewhere in this caseiranges from0to2. (As well asmatch.Groups[1].Valuefor the first group.)(+1 for question, I learned something today!)