7

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? :)

4 Answers 4

4

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!)

3
  • +1 I was not sure if .net has this feature and was too lazy to check.
    – stema
    Nov 19, 2012 at 8:47
  • @stema I didn't even realise this was an issue, I thought all the values would turn up in Groups!
    – Rawling
    Nov 19, 2012 at 8:48
  • Thanks for pointing me at .Captures collection! Eventually I came up with using same regexp as I put in the question and then I do for (int i = 1; i < match.Groups.Count; i++) foreach (var capture in match.Groups[i].Captures) { ... do smth with capture ... }
    – bazzilic
    Nov 19, 2012 at 9:37
3

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();
3
  • 2
    PS: This is a good site to test regular expressions in .NET: Regex Hero Nov 19, 2012 at 9:07
  • 1
    I like this one. Nov 19, 2012 at 9:52
  • Thanks @bazzilic and ChankeyPathak, I didn't know those websites. Nov 21, 2012 at 14:03
2

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*.

2

Simple regexp:

(?:^| *)(.+?)(?:,|$)

Explanation:

?:    # Non capturing group
^| *  # Match start of line or multiple spaces
.+    # Capture the word in the list, lazy
?:    # Non capture group
,|$   # Match comma or end of line 

Note: Rublular is a nice website for testing this kind of thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.