title: Capture group multiple times
body: <p>Lately I have being playing around with regex in Java, and I find myself into a problem which (theoretically) is easy to solve, but I was wandering if there is any easier way to do it (Yes, yes I am lazy), the problem is <strong>capture a group multiple times</strong>, this is:</p>
  
  <pre><code>public static void main(String[] args) {
      Pattern p = Pattern.compile("A (IvI(.*?)IvI)*? A");
      Matcher m = p.matcher("A IvI asd IvI IvI qwe IvI A"); //ANY NUMBER of IvI x IvI
      //Matcher m = p.matcher("A  A");
      int loi = 0; //last Occurrence Index
      String storage;
      while (loi &gt;= 0 &amp;&amp; m.find(loi)) {
          System.out.println(m.group(1));
          if ((storage = m.group(2)) != null) {
              System.out.println(storage);
          }
          //System.out.println(m.group(1));
          loi = m.end(1);
      }
      m.find();
      System.out.println("2 opt");
      Pattern p2 = Pattern.compile("IvI(.*?)IvI");
      Matcher m2 = p2.matcher(m.group(1)); //m.group(1) = "IvI asd IvI IvI qwe IvI"
      loi = 0;
      while (loi &gt;= 0 &amp;&amp; m2.find(loi)) {
          if ((storage = m2.group(1)) != null) {
              System.out.println(storage);
          }
          loi = m2.end(0);
      }
  }
  </code></pre>
  
  <p>Using <strong>ONLY</strong> <code>Pattern p</code> <strong>is there any way to get what is inside <code>IvI's</code>?</strong> (in the test string would be "asd" and "qwe") considering that there could be any number of <code>IvI's</code> sections, something alike of what I am trying to do in the first while which is, finding the first occurrence of the group, then moving the index and search for the next group and so on and so on...</p>
  
  <p>Using the code I wrote in that while it returns <code>asd IvI IvI qwe</code> as the group 2, not just <code>asd</code> and then <code>qwe</code>, in part I suppose it could be because of the (.*?) part, is is not supposed to be greedy but still it goes up to the <code>qwe</code> consuming two of the <code>IvI's</code>, I mention this because otherwise I may be able to use the end index of those with the <code>matcher.find(anInt)</code> method, but it does not work either; I don't think it is anything wrong with the regex, since the next code works without consuming the <code>IvI</code>.</p>
  
  <pre><code>public static void main(String[] args) {
      Pattern p = Pattern.compile("(.*?)IvI");
      Matcher m = p.matcher("bla bla blaIvI");
      m.find();
      System.out.println(m.group(1));
  }
  </code></pre>
  
  <p>This prints: <code>bla bla bla</code></p>
  
  <p>THERE IS A SOLUTION I KNOW (but I am lazy remember)</p>
  
  <p>(Also on the first code, bellow "2 opt" message)
  The solution is dividing it into sub-groups and use another regex where you process only those sub-groups one at a time...</p>
  
  <p>BTW: I did my homework
  In <a href="http://www.rexegg.com/regex-capture.html" rel="nofollow">this</a> page it mentions</p>
  
  <blockquote>
    <p>Since a capture group with a quantifier holds on to its number, what value does the engine return when you inspect the group? All engines return the last value captured. For instance, if you match the string A_B_C_D_ with ([A-Z]<em>)+, when you inspect the match, Group 1 will be D</em>. With the exception of the .NET engine, all intermediate values are lost. In essence, Group 1 gets overwritten each time its pattern is matched.</p>
  </blockquote>
  
  <p>But I am still hoping you to give me good news...</p>