title: Scanning list with Java regex grouping
body: <p>I want to get the positions and lengths of the elements in a list. Therefore, I would like to use regex grouping because with that I'm able to get the offsets and lengths of the matched groups. The elements are separated with commas and are allowed to contain any kind of characters. Here's an example:</p>
  
  <p>( 1234, A{}, "foo" )</p>
  
  <p>Here is what I tried to do:</p>
  
  <pre><code>String textToMatch = "( 1234, A{}, \"foo\" )";
  Pattern p = Pattern.compile("\\(\\s*([^,]+?)(?:\\s*,\\s*([^,]+?)\\s*)*\\)");
  Matcher m = p.matcher(textToMatch);
  
  if (m.find()) {
      for (int i = 1; i &lt;= m.groupCount(); i++) {
          System.out.println(m.group(i));
      }
  }
  </code></pre>
  
  <p>Unfortunately, this does not work. I only get the first and last element, but not the ones in between. Here's the output I get with the code shown above:</p>
  
  <pre><code>1234
  "foo"
  </code></pre>
  
  <p>If I use the same regex three times instead of using the star operator it works. I guess I do something wrong with iterating through the groups. Any ideas?</p>